Android activity lifecycle

Thinuwan Wickrama Arachchi
3 min readMay 7, 2018

Here you can get a simple idea about the android activity lifecycle and lifecycle callback methods.

Following are the main lifecycle callback methods.

01. onCreate()
02. onStart()
03. onResume()
04. onPause()
05. onStop()
06. onDestroy()
07. onRestart()
08. onSaveInstanceState()
09. onRestoreInstanceState()

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Activity lifecycle","onCreate");
}

@Override
protected void onStart() {
super.onStart();
Log.i("Activity lifecycle","onStart");
}

@Override
protected void onResume() {
super.onResume();
Log.i("Activity lifecycle","onResume");
}

@Override
protected void onPause() {
super.onPause();
Log.i("Activity lifecycle","onPause");
}

@Override
protected void onStop() {
super.onStop();
Log.i("Activity lifecycle","onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i("Activity lifecycle","onDestroy");
}

@Override
protected void onRestart() {
super.onRestart();
Log.i("Activity lifecycle","onRestart");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("Activity lifecycle","onSaveInstanceState");
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i("Activity lifecycle","onRestoreInstanceState");
}

among above, 3 callbacks(onCreate(), onSaveInstanceState(), onRestoreInstanceState()) receive a parameter which is a Bundle object.

Android activity lifecycle (https://developer.android.com/guide/components/activities/activity-lifecycle)

onCreate()

Fires when the system first creates the activity. So onCreate() calls only once in the entire life of the activity. So you have to do stuffs like declaring the user interface, performing basic application logic, initializing background threads, instantiating class scope variables inside onCreate() method. This method receives the parameter savedInstanceState which contains previously saved state. If the activity has never exists before the value is null.

onStart()

onStart() calls when the activity is getting visible to user. ex:- when activity loading for first time, coming back from another activity, coming foreground from minimized state, in screen rotation, when turn on the display from sleep state etc. If you are using BroadcastReceivers you have to register them here.

onResume()

This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such as receiving a phone call. You should implement onResume() to initialize components that you release during onPause().

onPause()

The system calls this method as the first indication that the user is leaving your activity. You have to manipulate the operations like pausing animations, music playback, camera, broadcastReceivers, GPS etc. to save system resources.

onStop()

When your activity is no longer visible to the user, it has entered the Stopped state. In the onStop() method, the app can release almost all resources that aren’t needed while the user is not using it. ex:- BroadcastReceiver.

onDestroy()

Called before the activity is destroyed. This is the final call that the activity receives. This callback mainly happens because the activity is finishing by calling finish(), or system is temporarily destroying the process to save space or in orientation change. The onDestroy() callback releases all resources that have not yet been released by earlier callbacks such as onStop().

onRestart()

onRestart() calls in the places like pressed home button and opened app again, coming back to the activity from another activity, when screen is turned back on. From this method you can specifically identify activity is starting for the first time or restarting.

onSaveInstanceState()

onSaveInstanceState() calls between onPause() and onStop(). This method mainly used to keep the state in orientation change. when you rotate the screen activity will destroy and create again. So the data you attached in runtime will be lost. By saving them in the bundle object you can access them after the screen rotation.

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(itemName, itemValue);
}

onRestoreInstanceState()

onRestoreInstanceState() calls between onStart(0 and onResume(). You can access the saved bundle object from this callback. You can access that bundle in onCreate() also but there you have to check for null.

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
value = savedInstanceState.getString(itemName);
textView.setText(value);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null){
value = savedInstanceState.getString(itemName);
textView.setText(value);
}

}

Following are some common activity lifecycles.

(https://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for)

Cheers …………….. !!!

--

--