You must use ActivityLifecycleCallbacks.
ActivityLifecycleCallbacks is interface of Application.class.
Can handle the called lifeCycle of Activity.
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | public class MyApplication extends Application { @Override public void onCreate (){ super .onCreate(); registerActivityLifecycleCallbacks( new MyActivityLifecycleCallbacks()); } @Override public void onTerminate (){ super .onTerminate(); } @Override public void onConfigurationChanged (Configuration newConfig){ super .onConfigurationChanged(newConfig); } private static final class MyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks { public void onActivityCreated(Activity activity, Bundle bundle) { Log.e( "" , "onActivityCreated:" + activity.getLocalClassName()); } public void onActivityDestroyed(Activity activity) { Log.e( "" , "onActivityDestroyed:" + activity.getLocalClassName()); } public void onActivityPaused(Activity activity) { Log.e( "" , "onActivityPaused:" + activity.getLocalClassName()); } public void onActivityResumed(Activity activity) { Log.e( "" , "onActivityResumed:" + activity.getLocalClassName()); } public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.e( "" , "onActivitySaveInstanceState:" + activity.getLocalClassName()); } public void onActivityStarted(Activity activity) { Log.e( "" , "onActivityStarted:" + activity.getLocalClassName()); } public void onActivityStopped(Activity activity) { Log.e( "" , "onActivityStopped:" + activity.getLocalClassName()); } } } |