Articles → ANDROID → Activity Life Cycle In Android

Activity Life Cycle In Android






Events




Picture showing the flow chart of activity life cycle in android
Click to Enlarge

  1. onCreate method
    1. This method executes immediately after the activity is launched.
    2. Any initialization in the code will come in this method.
  2. onStart method - This method is executed
    1. After the onCreate method.
    2. When activity is visible to the user.
  3. onResume method
    1. This method is called after onRestart and onPause method.
    2. This method indicates that activity is active and ready to take inputs.
  4. onPause method
    1. This method in invoked when activity is in background and not visible to the user.
  5. onStop method
    1. This method in executed when activity is in background and not visible to the user (same as onPause).
    2. This method is executed after the onPause method.
    3. Operations like writing data into the database could be written inside this method.
  6. onDestroy method
    1. This method is executed before the activity is finished.
    2. Activities could be finished because the programmer is finishing it explicitly or system is destroying the instance to save space.
  7. onRestart method
    1. This method is executed once the activity is started after onStop method.
    2. Once the method is executed, the onStart method is called immediately.

  8. Example




    package com.example.demo_activity;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Log.v("METHOD", "onCreate method called");
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.v("METHOD", "onStart method called");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.v("METHOD", "onResume method called");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.v("METHOD", "onPause method called");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.v("METHOD", "onStop method called");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.v("METHOD", "onDestroy method called");
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.v("METHOD", "onRestart method called");
        }
    }



    Output


    -- ON LOAD OF ACTIVITY FOR THE FIRST TIME
    2019-08-27 15:42:40.878 10837-10837/com.example.demo_activity V/METHOD: onCreate method called
    2019-08-27 15:42:40.893 10837-10837/com.example.demo_activity V/METHOD: onStart method called
    2019-08-27 15:42:40.896 10837-10837/com.example.demo_activity V/METHOD: onResume method called
    
    -- ON MINIMIZING THE ACTIVITY
    2019-08-27 15:42:50.025 10837-10837/com.example.demo_activity V/METHOD: onPause method called
    2019-08-27 15:42:50.064 10837-10837/com.example.demo_activity V/METHOD: onStop method called
    
    -- ON RESTARTING THE ACTIVITY AFTER MINIMIZING
    2019-08-27 15:42:52.878 10837-10837/com.example.demo_activity V/METHOD: onRestart method called
    2019-08-27 15:42:52.882 10837-10837/com.example.demo_activity V/METHOD: onStart method called
    2019-08-27 15:42:52.884 10837-10837/com.example.demo_activity V/METHOD: onResume method called
    
    -- ON CLOSING THE APPLICATION
    2019-08-27 15:44:32.599 10837-10837/com.example.demo_activity V/METHOD: onDestroy method called
    



    Posted By  -  Karan Gupta
     
    Posted On  -  Friday, September 20, 2019
     
    Updated On  -  Friday, April 3, 2020

    Query/Feedback


    Your Email Id
     
    Subject
     
    Query/FeedbackCharacters remaining 250