• The Android Lifecycle Cheat sheet — Part I: Single Activities
  • Jose Alcerreca
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: IllllllIIl
  • Proofread by: tanglie1993, atuooo

The Android system is designed to give users more control and make it easier to use applications. For example, a user of an app might rotate the screen, reply to a notification, or switch to another task, and the user should be able to continue using the app smoothly after such actions.

To provide this user experience, you should know how to manage the component lifecycle. A component can be an Activity, a Fragment, a Service, an Application itself, or even a silently running process. Components have a life cycle, and the life cycle can change in many states. When the state changes, the system notifies you through a lifecycle callback method.

To better explain how the lifecycle works, we defined a series of user scenarios that are categorized according to existing components.

Part I: Activities – The life cycle of a single activity (this article)

Part TWO: Multiple Activities– Jump and return to the back stack

Part three: Fragments– Life cycles of activities and fragments

Their charts are also provided with memos in PDF format for easy reference.


Unless otherwise noted, the following scenarios show the default behavior of these components.

If you find something wrong or missing something important, please comment below.

Part ONE: Activities

Single Activity – Scenario 1: The application is terminated and restarted

Trigger cause:

  • The user pressed the back key, or
  • Activity.finish()Method called

The simplest scenario illustrates what happens when a single activity application is started, ended, and restarted by the user:

Scenario 1: The application is terminated and restarted

State handling

  • OnSaveInstanceState will not be called (you don’t need to save the state because the activity is finished)
  • OnCreate doesn’t have a Bundle object if you reopen the application. Because the previous activity ended, there is no need to restore the state.

Single Activity – Scenario 2: Switch the user out

Trigger cause:

  • The user pressed the Home button
  • The user switches to another application (click on the virtual button (Overview Menu), click on a notification, answer a call, etc.)

Scenario 2: The user is switched out

In this scenario, the system stops the activity, but does not end it immediately.

State handling

When your activity goes into the Stopped state, the system uses onSaveInstanceState to save the application’s state in case the system terminates the application after a while (see below).

Assuming the application process is not terminated, the instance of the application is resident in memory, holding all state. When the activity returns to the foreground, it restores these states. You do not need to reinitialize these previously generated components.


Single Activity – Scenario 3: Configuration changes

Trigger cause:

  • Configuration changes, such as screen rotation
  • In multi-window mode, the user resizes the window

Scenario 3: Screen rotation or other configuration changes

State handling

Such configuration changes, like screen rotation or window size changes, should allow users to continue to use them seamlessly.

  • The activity is completely destroyed, but the state of the activity is saved and restored in the next instance.
  • inonCreateonRestoreInstanceStateBundle objects are the same in.

Single Activity – Scenario 4: The application is suspended

Trigger cause:

  • Open multi-window mode (API 24+) and app loses focus
  • Another application partially overrides the running application (e.g. a purchase dialog, a runtime permission confirmation dialog, a third-party login dialog…).
  • Call the intent selector, for example, call the share dialog

Scenario 4: The application is suspended

This scenario does not apply to:

  • The dialog boxes belong to the same application. Popping up an alert dialog or a DialogFragment does not pause blocked activities (executing onPause).
  • Notice. A blocked activity is not suspended when the user receives a new notification or pulls down the notification bar.

read

  • Android Life Cycle Memo Part 2 — Multiple Activities

The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.