avatarJose Alcérreca

Summary

This article discusses the Android Lifecycle cheat sheet for multiple activities, focusing on navigation and back stack.

Abstract

The article is part of a series on the Android Lifecycle cheat sheet. This part focuses on multiple activities and their lifecycle, including navigation and back stack. It explains how activities are stopped and destroyed when a new activity is started, and how the Back button affects the lifecycle. The article also covers managing state, configuration changes, and the app's process being killed. It emphasizes the importance of saving and restoring state in various scenarios. The article includes diagrams for better understanding and links to other parts of the series and related articles.

Bullet points

  • The article is part of a series on the Android Lifecycle cheat sheet.
  • This part focuses on multiple activities and their lifecycle.
  • When a new activity is started, the previous activity is stopped but not destroyed.
  • The Back button destroys and finishes the current activity.
  • Saving state is important for the activity in the foreground and all activities in the stack.
  • The system can kill the app's process at any time, so state needs to be restored in any situation.
  • The article includes diagrams for better understanding.
  • The article links to other parts of the series and related articles.

The Android Lifecycle cheat sheet — part II: Multiple activities

In this series: * Part I: Activities — single activity lifecycle * Part II: Multiple activities — navigation and back stack (this post) * Part III: Fragments — activity and fragment lifecycle * Part IV: ViewModels, Translucent Activities and Launch Modes

The diagrams are also available as a cheat sheet in PDF format for quick reference.

Note that, when showing lifecycles for multiple components (activities, fragments, etc) in a diagram, grouped events that appear side by side run in parallel. The execution focus can switch from one parallel group of events to another at any time, so the order of calls among parallel groups of events is not guaranteed. However, order inside a group is guaranteed.

The following scenarios don’t apply to activities and tasks that have a custom launch mode or task affinity defined. For more information, see Tasks And Back Stack on the Android developer website.

Back Stack — Scenario 1: Navigating between activities

Scenario 1: Navigating between activities

In this scenario, when a new activity is started, activity 1 is STOPPED (but not destroyed), similar to a user navigating away (as if “Home” was pressed).

When the Back button is pressed, activity 2 is destroyed and finished.

Managing state

Note that onSaveInstanceState is called, but onRestoreInstanceState is not. If there is a configuration change when the second activity is active, the first activity will be destroyed and recreated only when it’s back in focus. That’s why saving an instance of the state is important.

If the system kills the app process to save resources, this is another scenario in which the state needs to be restored.

Back Stack — Scenario 2: Activities in the back stack with configuration changes

Scenario 2: Activities in the back stack with configuration changes

Managing state

Saving state is not only important for the activity in the foreground. All activities in the stack need to restore state after a configuration change to recreate their UI.

Also, the system can kill your app’s process at almost any time so you should be prepared to restore state in any situation.

Back Stack — Scenario 3: App’s process is killed

When the Android operating system needs resources, it kills apps in the background.

Scenario 3: App’s process is killed

Managing state

Note that the state of the full back stack is saved but, in order to efficiently use resources, activities are only restored when they are recreated.

Read also

Continue reading

Android App Development
Recommended from ReadMedium