Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to catch activities in foreground/background using ActivityLifecycleCallbacks?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.02k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: