Android Lifecycle Scenarios – Single and Multi Activities
Tags: technology, android, kotlin
Published: 2021-05-18 00:19:45

---
title: Android Lifecycle Scenarios – Single and Multi Activities
published: true
date: 2021-04-29 01:04:20 UTC
tags: Technology,android,kotlin
canonical_url: https://victorbrandalise.com/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](https://victorbrandalise.com/android-lifecycle-revised/ "https://victorbrandalise.com/android-lifecycle-revised/")_.

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](https://miro.medium.com/max/383/1*U_j3OP74jrPFoNvO2i7XzQ.png)

`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](https://miro.medium.com/max/482/1*DCo7awxJ3KhnW88h365vhA.png)

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](https://miro.medium.com/max/441/1*3qxYnT2vRwrQVORi9mfUhw.png)

`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](https://miro.medium.com/max/523/1*j3blnCW082yMbQe5fkjMMg.png)

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

- [The Android Lifecycle cheat sheet — part I: Single Activities](https://medium.com/androiddevelopers/the-android-lifecycle-cheat-sheet-part-i-single-activities-e49fd3d202ab)
- [Lifecycle methods of Android Activity — Scenario based Question](https://medium.com/proggy-blast/lifecycle-methods-of-android-activity-scenario-based-question-8d3e14169297)
- [onSaveInstanceState () and onRestoreInstanceState ()](https://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate)

Cover Photo by [Laura Ockel](https://unsplash.com/@viazavier?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/gear?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

The post [Android Lifecycle Scenarios – Single and Multi Activities](https://victorbrandalise.com/android-lifecycle-scenarios-single-and-multi-activities/) first appeared on [Victor Brandalise](https://victorbrandalise.com).