Android Lifecycle Scenarios – Single and Multi Activities

These are my personal notes on the topic. Most of the content here is not mine. All sources can be found at the end of the article.

Activities are a main part of Android. Lifecycle methods is the way Android notifies state changes to the Activity. To provide a great user experience, you should know how to manage them.

If you haven’t read my article about Activity Lifecycles, you can read it here.

Lets take a look at some common scenarios using a single Activity

1. App is finished or restarted

This scenario happens when there’s only one activity in the back stack and:

  • The user presses the Back button
  • The Activity.finish() method is called
App is finished or restarted

onSaveInstanceState is not called.
onCreate doesn’t have a Bundle when the app is reopened.

If there’s only one activity in the back stack, the app will be finished. To avoid that you can override onBackPressed(), that way onDestroy() won’t get called.

@Override
public void onBackPressed() {
   moveTaskToBack(true);
}

2. Configuration changes

This scenario happens when there’s only one activity in the back stack and:

  • The configuration changes, like a rotation
  • User resizes the window in multi-window mode (available in android 7.0)
Configuration changes

The activity is completely destroyed, but the state is saved and restored for the new instance.
The Bundle in onCreate() and onRestoreInstanceState() is the same.

Why is the Bundle sent to two methods?

onRestoreInstanceState() is called only when recreating activity after it was killed by Android. It is possible to restore the state in onRestoreInstanceState(), but not very common. onRestoreInstanceState() is called after onStart().

Usually you restore your state in onCreate(). onCreate() is called before onStart().


3. User navigates away

This scenario happens when there’s only one activity in the back stack and:

  • The user presses the Home button
  • The user switches to another app

For example, you’re browsing Twitter and you click on a notification that takes you to another app. When you switch back to Twitter the activity’s onRestart() will called.

User navigates away

onSaveInstanceState() is used to save the app state in case the system kills the app’s process later on.

This scenario is very similar to navigating from Activity A to Activity B, the only difference is that onSaveInstanceState() is not called when navigating between activities.


4. App is paused by the system

  • Enabling Multi-window mode (API 24+) and losing the focus
  • Another app partially covers the running app (a purchase dialog, a runtime permission dialog, a third-party login dialog…)
  • An intent chooser appears, such as a share dialog
App is paused by the system

Doesn’t apply to:

  • Dialogs in the same app. Showing an AlertDialog or a DialogFragment won’t pause the underlying activity.
  • Notifications. User receiving a new notification or pulling down the notification bar won’t pause the underlying activity.

Now lets take a look at some common scenarios using multiple Activities

1. Navigate from Activity A to Activity B

When activity A is created, onCreate() is called. Then onStart() is called. Once the user is able to interact with the app, onResume()  is called. Now the user opens Activity B.

onPause() of activity A will get called. Then activity B’s onCreate() followed by onStart() and onResume(). Finally activity A’s onStop() is triggered.

Here is the callbacks order

Activity A -> onCreate() , onStart(), onResume(), <Activity B started>, onPause()

Activity B -> onCreate(), onStart(), onResume()

Activity A -> onStop(), onSaveInstanceState()


2. Navigate from Activity B back to Activity A

When the back button is pressed, Activity B’s onPause() is triggered. Then activity A’s onRestart() is called followed by onStart() and onResume(). After that activity B’s onStop() gets called followed by onDestroy().

Here is the callbacks order

Activity B -> <Back is pressed>, onPause()

Activity A -> onRestart(), onStart(), onResume()

Activity B -> onStop(), onDestroy()


Thank you for reading.

Sources

Cover Photo by Laura Ockel on Unsplash