To catch the states of every activity in our android application we have to implement ActivityLifecycleCallbacks. ActivityLifecycleCallbacks will have different override method as onActivityCreated, onActivityStarted, onActivityResumed, onActivityPaused, onActivityStopped, onActivitySavedInstanceState on ActivityDestroyed.
Using these overridden methods we will call the class/activity name and get the different state inside our logs.
public class MyLifeCycle implements Application.ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.i(activity.getLocalClassName(),"just created");
}
@Override
public void onActivityStarted(Activity activity) {
Log.i(activity.getLocalClassName(), "is started");
}
@Override
public void onActivityResumed(Activity activity) {
Log.i(activity.getLocalClassName(), "is resumed");
}
@Override
public void onActivityPaused(Activity activity) {
Log.i(activity.getLocalClassName(), "is in paused state");
}
@Override
public void onActivityStopped(Activity activity) {
Log.i(activity.getLocalClassName(),"stopped");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.i(activity.getLocalClassName(),"SaveInstanceState");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.i(activity.getLocalClassName(),"is destroyed");
}
}
Add this MyApplication class in your manifest class under application tag, this will give you the different states of activities used in the application.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new MyLifeCycle());
}
}
0 Comment(s)