Android Activity lifecycle scenario’s
Android activities are the UI component that interact with the user. It allows all the view and viewgroup to display over UI. Mainly it is the fist interacting component of android application.
ByDefault we have one activity got created with project that is also launcher activity.
Lifecycle methods of Android activities
- OnCreate() : When activity just got created.
- onStart() : When activity got visible.
- onResume() : When activity start interacting.
- onPause(): When activity stop interacting.
- onStop(): When activity got invisible.
- onRestart() : When activity got restarted after a onStop scenario.
- onDestroy(): When activity got finished.
Lifecycle Scenario’s of Android activities
- When android activity launches, below lifecycle methods call in sequence
2. When you press back button, below lifecycle methods call in sequence
Case I : Android Device ≥ Android 12
Case II : Android Device < Android 12
3. When we press home button, below lifecycle methods call in sequence
4. When you open the app from recent apps, below lifecycle methods call in sequence
5. When activity is in active state and we press recent button, below lifecycle methods call in sequence
Case I : when device is ≥ Android 12 then
No Lifecycle method will call
Case II : when device is < Android 12 then
6. When you have 2 different activities namely A, B and Scenario is moving from Activity A to Activity B, below lifecycle methods call in sequence
7. When you are at Activity B and now you press back and get back again to activity A then, below lifecycle methods call in sequence
Note — if your activity is not launcher activity then after onPause(), onDestroy() will also call next to onPause().
8. Rotation of Activity Scenario’s
Case-I
When you are at Activity A is on screen and is rotated then, below Lifecycle methods call in sequence
Case II
When you are at Activity A is on screen and Fragment A is hosted on it statically then screen got rotated then, below Lifecycle methods call in sequence
9. When you open a Alertdialog on activity?
May be you think that onPause() should invoke once Alertdialog will shown as you can see focus is top over activity only on dialog but actually you are wrong.
Note: No lifecycle method will call when Alertdialog will shown on activity.
10. When screen got locked then what lifecycle method should call of Activity?
11. When your Screen got unlocked then what lifecycle method should call of Activity?
12. When you open a bottom sheet dialog then?
May be you think that onPause() should invoke once bottom sheet dialog will shown as you can see focus is top over activity only on dialog but actually you are wrong.
Note: No lifecycle method will call when bottom sheet dialog will shown on activity.
13. When your device got low battery and you enabled power saving mode then lifecycle methods of activity is
14. When switch from current application to another application by tasks
15. When switch back to your app from last app then lifecycle methods are
Upnext Topic: Fragments
Thank you for reading my article. I really appreciate your response.
Clap if this article helps you. If I got something wrong, please comment for improve.
let’s connect on Linkedin , GitHub