Articles → ANDROID → Activity Life Cycle In Android
Activity Life Cycle In Android
Events
Click to Enlarge
- onCreate method
- This method executes immediately after the activity is launched.
- Any initialization in the code will come in this method.
- onStart method - This method is executed
- After the onCreate method.
- When activity is visible to the user.
- onResume method
- This method is called after onRestart and onPause method.
- This method indicates that activity is active and ready to take inputs.
- onPause method
- This method in invoked when activity is in background and not visible to the user.
- onStop method
- This method in executed when activity is in background and not visible to the user (same as onPause).
- This method is executed after the onPause method.
- Operations like writing data into the database could be written inside this method.
- onDestroy method
- This method is executed before the activity is finished.
- Activities could be finished because the programmer is finishing it explicitly or system is destroying the instance to save space.
- onRestart method
- This method is executed once the activity is started after onStop method.
- Once the method is executed, the onStart method is called immediately.
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