Android Activity Lifecycle Flashcards
What is an activity in the Android operating system?
An activity is a single screen of an app. An app may have many activities, but only one activity may be displayed at once.
What is the Android Activity Lifecycle?
The activity lifecycle is a set of callback methods that are called as the Android OS handles each currently instantiated activity.
What methods are called when an app is ran for the first time?
onCreate(), onStart(), then onResume().
Creates the activity, prepares it for being visible, then prepares it for being interactable.
What methods are called when you press the home button from within an app, then open it back up again?
onPause(), onStop(), onRestart(), onStart() then onResume()
Prepares the activity for the background.
When the app is reopened, the app prepares for being visible again, then prepares its interactable elements.
What methods are called when you forcibly kill an app, then restart it?
onPause(), onCreate(), onStart() then onResume()
Prepares the activity for the background. When we kill the app, it interrupts the lifecycle, returning it to onCreate(), where the app is prepared and made visible again.
What is the purpose of onCreate() in the Android Activity Lifecycle?
Calls super.onCreate(), then performs any additional functions that need to be performed alongside this, before the activity is visible.
What is the purpose of onStart() in the Android Activity Lifecycle?
onStart() prepares any visible-only behaviors, and loads the persistent app state.
What is the purpose of onResume() in the Android Activity Lifecycle?
Prepares any foreground-only behaviors that may need to be interacted with, just before it becomes interactable.
What is the purpose of onPause() in the Android Activity Lifecycle?
Shuts down any foreground-only behaviors, and saves the persistent app state, sending the app into a paused state.
If the app is killed in a paused state, the activity will need to be prepared again from onCreate().
What is the purpose of onStop() in the Android Activity Lifecycle?
Caches the activity state.
What is the purpose of onDestroy() in the Android Activity Lifecycle?
Releases any activity resources being used by the application.
What is the purpose of onRestart() in the Android Activity Lifecycle?
To perform any actions that need to be performed when the app is closed and reloaded - for example, refiring any API requests.
What methods are called when you navigate back to an activity from another?
onRestart(), onStart() and onResume()
I’m not including onPause here, since we assume that it’s already been paused.
The activity is restarted, then prepared for visibility and interactability again.
Why is only onPause() called when the app is forcibly killed by the OS?
onPause is called to save the persistent app state and to close any foreground-only behaviours. This is the only action we need to do before killing the app forcibly.
We don’t need to cache the activity state with onStop(), since we’re not going to be opening the same activity state ever again.