Android Activity Lifecycle

zmbailey

Zander Bailey

Posted on March 16, 2020

Android Activity Lifecycle

Activities are the building blocks of and Android application, and in order to make the most use out of an Activity it is necessary to understand the stages of the Android Activity Lifecycle. Understanding the stages will allow you to anticipate state changes, and make sure that operations happen at the right time so that states are preserved properly. There are six basic transitions between stages, each represented by a function that allows you to perform operations upon transition: onCreate(), onStart(), onResume, onPause, onStop, and onDestroy.

onCreate()

onCreate() is the first and possibly the most important, and actually must be implemented. It runs when the system creates the activity, and contains code to set up content and initial interaction. onCreate() should contain basic startup logic that only happens once for span of the activity’s life. Often times onCreate() will contain code to initialize the xml for the activity layout, or code to create custom Views instead. With onCreate() the activity enters the ‘Created’ state.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Here you can see how the onCreate() function uses a saved state to set up the activity defaults, and then uses a separately defined layout file to set the activity content.

onStart()

onStart() happens once the Activity has finished creating everything. OnStart() can be used in a similar fashion to onCreate() at times, although setting the layout should be confined to onCreate(). onStart() can contain such operations as setting listeners or working with objects that require the layout to already be established. When onStart() occurs the activity will become visible and the app prepares to become interactive. onStart() sends the Activity in to the ‘Started’ state.

protected void onStart() {
    super.onStart();
}

onStart() is less commonly used, since many functions that might be performed at this stage could also be performed during onCreate().

onResume()

onResume() is the last transition before the activity is fully running, and it is a more common place to make adjustments or to load preserved data. This runs when the Activity enters the Resumed state, and it comes to the foreground. Now the app is operational, and it will remain in this state until something occurs to change focus away from the app.

protected void onResume() {
    super.onResume();
}

The app is now fully operational and interacting with the user. So far we have Created->Started->Resumed. Now that the app is running, we’re going to look at the stages of shutting down.

onPause()

onPause is the first stage, where focus has been removed from the Activity. The ‘Paused’ state indicates that the Activity is no longer in the foreground. onPause() may be used to adjust or suspend operations that need to be altered while in the ‘Paused’ state. When the Activity returns to the foreground it will trigger onResume() again as the app returns to the ‘Resumed’ state.

protected void onPause() {
    super.onPause();
}

The Paused state can occur without leaving or stopping the app, so onPause() and onResume() should be paid close attention to if there are issues when resuming an Activity.

onStop()

Like it sounds, onStop() occurs when Activity is finished running and is preparing to terminate. It is also triggered when the Activity is no longer visible, for instance when another Activity is launched that covers the entire screen. onStop() may be a good time to make sure you have preserved the state of your Activity, or take care of other things that need to be done before the app terminates.

protected void onStop() {
    super.onStop();
}

There is actually one more transition, if the Activity is in the Stopped state and the user navigates back to it, it will trigger the onRestart() before triggering onStart() again. onRestart() is somewhat redundant, except that it allows the option to call functions when restarting that are not called on initial start.

onDestroy()

onDestroy() is invoked when the Activity is about to be destroyed. This can happened for two reasons: if the activity is finishing, or when the Activity is temporarily destroyed because of a change in configuration, like rotating the device. If necessary it is possible to distinguish between the two versions by calling isFinishing() to tell if it is finishing or reconfiguring. onDestroy() can be used to release any resources that may not have been automatically released by destroying the Activity.

protected void onDestroy() {
    super.onDestroy();
}

Some of these functions are used often when working with Activities, and some are rarely used at all. It depends on the design of your application, as well as your approach. Regardless of if you intend to use them, it is still handy to know exactly what is happening behind your Activities. Debugging transitions can go a lot smoother if you understand exactly when things need to be updated so that you don’t lose data or try to affect things that don’t exist yet.

💖 💪 🙅 🚩
zmbailey
Zander Bailey

Posted on March 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related