Android Activity Lifecycle Flashcards

1
Q

What is an activity in the Android operating system?

A

An activity is a single screen of an app. An app may have many activities, but only one activity may be displayed at once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the Android Activity Lifecycle?

A

The activity lifecycle is a set of callback methods that are called as the Android OS handles each currently instantiated activity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What methods are called when an app is ran for the first time?

A

onCreate(), onStart(), then onResume().

Creates the activity, prepares it for being visible, then prepares it for being interactable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What methods are called when you press the home button from within an app, then open it back up again?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What methods are called when you forcibly kill an app, then restart it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of onCreate() in the Android Activity Lifecycle?

A

Calls super.onCreate(), then performs any additional functions that need to be performed alongside this, before the activity is visible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of onStart() in the Android Activity Lifecycle?

A

onStart() prepares any visible-only behaviors, and loads the persistent app state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of onResume() in the Android Activity Lifecycle?

A

Prepares any foreground-only behaviors that may need to be interacted with, just before it becomes interactable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of onPause() in the Android Activity Lifecycle?

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of onStop() in the Android Activity Lifecycle?

A

Caches the activity state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of onDestroy() in the Android Activity Lifecycle?

A

Releases any activity resources being used by the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of onRestart() in the Android Activity Lifecycle?

A

To perform any actions that need to be performed when the app is closed and reloaded - for example, refiring any API requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What methods are called when you navigate back to an activity from another?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is only onPause() called when the app is forcibly killed by the OS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly