Android’s Activity Life-cycle
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.
Let’s understand the lifecycle
The Activity lifecycle consists of 7 methods:
- 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.
- onStart(): This method is called when an activity becomes visible to the user and the activity enters the Started state.
- 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.
- 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).
- 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.
- onRestart(): It is called when the activity in the stopped state and is about to start again.
- 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
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.)
Single Activity — Scenario 3: Configuration changes
Triggered by:
- Configuration changes, like a rotation
- User resizes the window in multi-window mode
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
This scenario doesn’t apply to:
- Dialogs in the same app. Showing an
AlertDialog
or aDialogFragment
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
Back Stack — Scenario 2: Activities in the back stack with configuration changes
Back Stack — Scenario 3: App’s process is killed
Activity with Fragment starts and finishes
Reference: https://developer.android.com/guide/components/activities/activity-lifecycle
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.