In large Android applications, developers often repeat the same code across multiple activities.
Examples:
- Progress dialogs
- Keyboard handling
- Activity transitions
- Internet connection checks
- Permission handling
- Font configuration
To avoid duplicate code and improve maintainability, Android applications commonly use:
BaseActivity
What Is BaseActivity?
BaseActivity is a parent activity class that contains:
- Common reusable functionality
- Shared UI behavior
- Global helper methods
- Application-wide configurations
Other activities extend BaseActivity to inherit common features.
Why Use BaseActivity?
Without BaseActivity:
- Code duplication increases
- Maintenance becomes difficult
- Bug fixing becomes repetitive
- Project structure becomes messy
BaseActivity centralizes shared functionality.
Common Features Inside BaseActivity
- Loading dialogs
- Network state detection
- Keyboard hiding
- Permission helpers
- Activity animations
- Analytics logging
- Crash handling
- Theme handling
Modern Android Recommendation
Older Android tutorials use:
android.support.*
which is deprecated.
Modern Android applications should use:
androidx.*
Modern BaseActivity Example
package com.example.baseapp;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public abstract class BaseActivity
extends AppCompatActivity {
@Override
protected void onCreate(
@Nullable Bundle savedInstanceState
) {
super.onCreate(savedInstanceState);
}
@Override
public void startActivity(
Intent intent
) {
super.startActivity(intent);
overridePendingTransition(
android.R.anim.fade_in,
android.R.anim.fade_out
);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(
android.R.anim.fade_in,
android.R.anim.fade_out
);
}
@Override
public boolean dispatchTouchEvent(
MotionEvent event
) {
View view = getCurrentFocus();
if (view instanceof EditText) {
int[] coordinates =
new int[2];
view.getLocationOnScreen(
coordinates
);
float x = event.getRawX()
+ view.getLeft()
- coordinates[0];
float y = event.getRawY()
+ view.getTop()
- coordinates[1];
if (
event.getAction()
== MotionEvent.ACTION_UP
) {
if (
x < view.getLeft()
|| x > view.getRight()
|| y < view.getTop()
|| y > view.getBottom()
) {
hideKeyboard(view);
}
}
}
return super.dispatchTouchEvent(
event
);
}
private void hideKeyboard(
View view
) {
InputMethodManager manager =
(InputMethodManager)
getSystemService(
Context.INPUT_METHOD_SERVICE
);
if (manager != null) {
manager.hideSoftInputFromWindow(
view.getWindowToken(),
0
);
}
}
}
Understanding BaseActivity Features
- Common transition animation
- Keyboard auto hide
- Centralized lifecycle behavior
- Shared utility methods
How to Use BaseActivity?
Instead of:
extends AppCompatActivity
activities should use:
extends BaseActivity
Example Usage
public class MainActivity
extends BaseActivity {
}
What Is AppController?
AppController is commonly an:
Application
class used for:
- Global initialization
- SDK setup
- Analytics setup
- Crash reporting
- Dependency initialization
Create AppController.java
package com.example.baseapp;
import android.app.Application;
public class AppController
extends Application {
private static AppController
instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static AppController
getInstance() {
return instance;
}
}
Add Application Class in Manifest
<application
android:name=".AppController"
android:allowBackup="true"
android:theme="@style/Theme.App">
</application>
Why Use Application Class?
Application class initializes components before activities start.
Useful for:
- Firebase
- Crashlytics
- Retrofit
- Dependency Injection
- Logging systems
Modern Android Replacements
Older tutorials commonly use:
- Fabric Crashlytics
- Calligraphy fonts
- Manual permission handlers
Modern Android now recommends:
- Firebase Crashlytics
- Material Typography
- Activity Result APIs
- Jetpack libraries
Modern Firebase Crashlytics
Instead of:
Fabric.with(this, new Crashlytics());
Modern apps use:
FirebaseCrashlytics
Modern Font Recommendation
Instead of Calligraphy library:
- Use XML fonts
- Use Material Typography
- Use Google Fonts
Modern Permission Handling
Instead of manual permission managers:
Use:
ActivityResultLauncher
Keyboard Handling Explanation
The:
dispatchTouchEvent()
method hides keyboard when user taps outside EditText.
This improves:
- User experience
- Form interaction
- Screen usability
Modern Architecture Recommendation
Modern Android apps usually combine:
- BaseActivity
- MVVM Architecture
- ViewBinding
- Dependency Injection
- Navigation Component
- Jetpack Compose
BaseActivity vs BaseFragment
| BaseActivity | BaseFragment |
|---|---|
| Activity-level logic | Fragment-level logic |
| Global UI behavior | Reusable fragment behavior |
| Transitions/navigation | Fragment-specific logic |
Modern Jetpack Compose Alternative
Jetpack Compose reduces the need for large BaseActivity classes by:
- Using composables
- Using state hoisting
- Using lifecycle-aware components
Common Beginner Mistakes
1. Putting Too Much Logic Inside BaseActivity
Keep BaseActivity lightweight and reusable.
2. Memory Leaks
Avoid storing Activity references globally.
3. Tight Coupling
Do not place feature-specific code inside BaseActivity.
Best Practices
- Keep BaseActivity generic
- Use helper methods carefully
- Avoid unnecessary inheritance
- Use lifecycle-aware components
- Combine with MVVM architecture
FAQ
Why use BaseActivity?
BaseActivity reduces duplicate code across multiple activities.
Is BaseActivity mandatory?
No. It is an architectural choice.
Should all activities extend BaseActivity?
Only if they share common reusable functionality.
Conclusion
BaseActivity is a powerful architectural pattern for organizing reusable Android application behavior.
It helps improve maintainability, reduce duplicate code, and centralize common functionality across activities.
Modern Android applications should combine BaseActivity, AndroidX, MVVM architecture, lifecycle-aware components, and scalable project structure for production-grade development.
About the Author
Salil Jha is a Full Stack and Mobile Developer specializing in Android, React Native, fintech systems, scalable SaaS platforms, and developer tooling products.
CodeChain Dev — Build Modern Products. Solve Real Problems.
No comments:
Post a Comment