android app update notification

Activity life cycle in Android | Illustrated using FlowChart

Activity life cycle in Android is an important concept in App Development that can help developers execute the right code as the activity visibility changes.

Activity life cycle in Android
Activity life cycle in Android

Activity Life cycle Methods

There are 7 activity life cycle methods. These methods can be overridden and developers can determine the code to be executed when a call to any of activity life cycle method is called by android.

The activity life cycle methods are as follows:

  1. onCreate(): The onCreate() method is called first when the activity is first created. Call to such method is important and all UI widgets are inflated in this method.
  2. onStart(): The onStart() method is called after the onCreate() method. Its when the activity becomes visible to the user, call to this life cycle method is made.
  3. onResume(): The onResume() method is called after the onStart() method. The code inside this method executes when the user starts interacting with the activity.
  4. onPause(): The onPause() method is called after onResume() method if the activity becomes invisible to the user. Trigger to such method is called when the user gets a phone call when your app activity is visible to the user or if the user switches to a different app or activity.
  5. onStop(): The onStop() method is called after the onPause() method when the activity is no longer visible to the user.
  6. onRestart(): The onRestart() method is called after the onStop() method if the user navigates back to the activity. The onRestart() triggers the call to onStart() and then onResume() method.
  7. onDestroy(): Last but the most important life cycle method. Call to onDestroy() method is called just before the activity is destroyed by the user or the system.
Activities life cycle in Android

The onCreate() and onDestroy() methods are called only once throughout the activity life cycle in android.

Life cycle Methods Java Code

    package com.androiddvlpr.activitylifecycle;  
      
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.util.Log;  
      
    public class MainActivity extends Activity {  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            Log.d("lifecycle","onCreate invoked");  
        }  
        @Override  
        protected void onStart() {  
            super.onStart();  
            Log.d("lifecycle","onStart invoked");  
        }  
        @Override  
        protected void onResume() {  
            super.onResume();  
            Log.d("lifecycle","onResume invoked");  
        }  
        @Override  
        protected void onPause() {  
            super.onPause();  
            Log.d("lifecycle","onPause invoked");  
        }  
        @Override  
        protected void onStop() {  
            super.onStop();  
            Log.d("lifecycle","onStop invoked");  
        }  
        @Override  
        protected void onRestart() {  
            super.onRestart();  
            Log.d("lifecycle","onRestart invoked");  
        }  
        @Override  
        protected void onDestroy() {  
            super.onDestroy();  
            Log.d("lifecycle","onDestroy invoked");  
        }  
    }  

As visible from the code above the usability of all seven activity life cycle method. Run the code above in Android Studio and you can view the Logs inside the methods called in the LogCat.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.