Visual aids of Android activity life cycle

An Android activity is a self-standing component of an Android application that can be started, stopped, paused, restarted, or reclaimed depending on various events including user-initiated and system-initiated ones.

Android activity life cycle

Image source.

onCreate (bundle savedInstanceState)

The activity’s life cycle starts with this method. In this method you should load your view hierarchies by loading the layouts into the content view of the activity. You also initialize any activity level variables that may be used during the lifetime of the activity. Like many of the callbacks you also call the parent’s onCreate() method first.

onStart()

After being created, this method pushes the activity into a visible state. In other words this method starts the visible life cycle of the activity. This method is called right after onCreate(). This method assumes that the view hierarchies are loaded and available from the onCreate().

onRestoreInstanceState(bundle savedInstanceState)

If the system choses to close the activity, because there is a change in orientation, then the user will expect that transitory (instance) state right back when the activity is restarted. To facilitate this, Android calls this onRestoreInstanceState method with a bundle that contains the saved instance state.

onResume()

The callback method onResume is the precursor to having the activity fully visible. This is also the start of the foreground cycle for the activity. In this foreground cycle the activity can move between onResume() and onPause() multiple times as other activities, notifications, or dialogs with more urgency come on top and go.

By the time this method is called, we can expect the views and their state fully restored. You can take this opportunity to tweak final state changes. As this method doesn’t have a bundle, you need to rely on the information from onCreate or onRestoreInstanceState methods to fine-tune state if further needed.

onPause()

This callback indicates that the activity is about to go into background. You should stop any counters or animations that were running when the activity was fully visible. The activity may go to onResume or proceed to onStop. Going to onResume will bring the activity to the foreground. Going to onStop will take the activity into a background state.

onStop()

The callback method onStop() moves the activity from partially visible to the background state while keeping all of the view hierarchies intact. This is the counterpart of onStart. The activity can be taken back to the visible cycle by calling onStart. This state transition of going from onStop to onStart during the same activity life cycle goes through the onRestart() method.

onSaveInstanceState(bundle saveStateBundle)

The control goes to onDestroy() coming out of onStop if the process is still in memory. However, if Android realizes that activity is being closed without the user’s expectation then it would call the onSaveInstanceState() before calling onDestroy(). Orientation change is a very concrete example of this. The SDK warns that the timing of onSaveInstanceState() is not predictable whether before or after onStop().

onRestart()

This method is called when the activity transitions from background state to partially visible state, i.e., going from onStop to onStart. You can use this knowledge in onStart if you want to optimize code there based on whether it is a fresh start or a restart. When it is a restart the view and their state are fairly intact.

onDestroy() onDestroy() is the counterpart ofonCreate(). The activity is going to finish afteronDestroy``. An activity can finish for two primary reasons. One is an explicit close. The second reason an activity can close is involuntary.

comments powered by Disqus