Android’s Activity Life-cycle

Life of Android Activity

Adil Khan
4 min readJul 23, 2020
Android activity

Well it’s not the tinder app but “any screen shown to the user is an activity”. In this case, we are seeing the log in activity of Tinder.

We know activity but what’s lifecycle?

Activity goes through many states. These states represents activity lifecycle. For example, a car has states like engine is off, engine is on but not moving, in motion, at rest, etc. You get the idea.

Activity lifecycle
A simplified illustration of the activity lifecycle from official documentation

Let’s understand the lifecycle

The Activity lifecycle consists of 7 methods:

  1. onCreate() : This method is called when the system first creates the activity. Here you perform basic application startup logic that should happen only once for the entire life of the activity.
  2. onStart(): This method is called when an activity becomes visible to the user and the activity enters the Started state.
  3. onResume(): It is called when the activity enters the Resumed state. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off.
  4. onPause(): It is called when the app is partially visible to the user on the mobile screen. The system calls this method as the first indication that the user is leaving your activity. It indicates that the activity is no longer in the foreground (though it may still be visible if the user is in multi-window mode).
  5. onStop(): It is called when the activity is no longer visible to the user. This may occur, for example, when a newly launched activity covers the entire screen. The app should release or adjust resources that are not needed while the app is not visible to the user. Using onStop() to free up resources instead of onPause() ensures that UI-related work continues, even when the user is viewing your activity in multi-window mode.
  6. onRestart(): It is called when the activity in the stopped state and is about to start again.
  7. onDestroy(): It is called when the activity is cleared from the application stack.

So, these are the 7 methods that are associated with the lifecycle of an activity.

Single Activity — Scenario 1: App is started for the very first time

Activity method flow

Single Activity — Scenario 2: User navigates away

Triggered by:

  • The user presses the Home button
  • The user switches to another app (via Overview menu, from a notification, accepting a call, etc.)
Activity method flow

Single Activity — Scenario 3: Configuration changes

Triggered by:

  • Configuration changes, like a rotation
  • User resizes the window in multi-window mode
Activity method flow

Single Activity — Scenario 4: App is paused by the system

Triggered by:

  • 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
Activity method flow

This scenario 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.

Note that, when showing lifecycles for multiple components (activities, fragments, etc.) in a diagram (below), grouped events that appear side by side run in parallel. 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.

Back Stack — Scenario 1: Navigating between activities

Activity method flow
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).

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

Activity method flow

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

Activity method flow

Activity with Fragment starts and finishes

Activity method flow

Reference: https://developer.android.com/guide/components/activities/activity-lifecycle

https://medium.com/androiddevelopers/the-android-lifecycle-cheat-sheet-part-i-single-activities-e49fd3d202ab

I hope you learned something! Thank you for reading.

You can find me on Twitter and Linkedin!

Click the 👏 to show your support and share it with other fellow Medium users.

--

--

Adil Khan

Mobile Application Enthusiast, an Android developer and a keen observer of life