Base 2 Flashcards

Lifecycle of an Activity

1
Q

Lifecycle of an Activity

A

OnCreate(): This is when the view is first created. This is normally where we create views, get data from bundles etc.
OnStart(): Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
OnResume(): Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
OnPause(): Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.
OnStop(): Called when you are no longer visible to the user.
OnDestroy(): Called when the activity is finishing
OnRestart(): Called after your activity has been stopped, prior to it being started again

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

What’s the difference between onCreate() and onStart()?

A

eate() method is called once during the Activity lifecycle, either when the application starts, or when the Activity has been destroyed and then recreated, for example during a configuration change.

The onStart() method is called whenever the Activity becomes visible to the user, typically after onCreate() or onRestart().

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

Scenario in which only onDestroy is called for an activity without onPause() and onStop()?

A

If finish() is called in the OnCreate method of an activity, the system will invoke onDestroy() method directly.

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

Why would you do the setContentView() in onCreate() of Activity class?

A

As onCreate() of an Activity is called only once, this is the point where most initialisation should go. It is inefficient to set the content in onResume() or onStart() (which are called multiple times) as the setContentView() is a heavy operation.

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

onSavedInstanceState() and onRestoreInstanceState() in activity?

A

OnRestoreInstanceState() - When activity is recreated after it was previously destroyed, we can recover the saved state from the Bundle that the system passes to the activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information. But because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

onSaveInstanceState() - is a method used to store data before pausing the activity.

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

Launch modes in Android?

A

1- Standard

2- SingleTop

3- SingleTask

4- SingleInstance

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

Launch modes in Android, Standard?

A

It creates a new instance of an activity in the task from which it was started. Multiple instances of the activity can be created and multiple instances can be added to the same or different tasks.
Eg: Suppose there is an activity stack of A -> B -> C.
Now if we launch B again with the launch mode as “standard”, the new stack will be A -> B -> C -> B.

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

Launch modes in Android, SingleTop?

A

It is the same as the standard, except if there is a previous instance of the activity that exists in the top of the stack, then it will not create a new instance but rather send the intent to the existing instance of the activity.
Eg: Suppose there is an activity stack of A -> B.
Now if we launch C with the launch mode as “singleTop”, the new stack will be A -> B -> C as usual.

Now if there is an activity stack of A -> B -> C.
If we launch C again with the launch mode as “singleTop”, the new stack will still be A -> B -> C.

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

Launch modes in Android, SingleTask?

A

A new task will always be created and a new instance will be pushed to the task as the root one. So if the activity is already in the task, the intent will be redirected to onNewIntent() else a new instance will be created. At a time only one instance of activity will exist.
Eg: Suppose there is an activity stack of A -> B -> C -> D.
Now if we launch D with the launch mode as “singleTask”, the new stack will be A -> B -> C -> D as usual.
Now if there is an activity stack of A -> B -> C -> D.
If we launch activity B again with the launch mode as “singleTask”, the new activity stack will be A -> B. Activities C and D will be destroyed.

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

Launch modes in Android, SingleInstance?

A

Same as single task but the system does not launch any activities in the same task as this activity. If new activities are launched, they are done so in a separate task.
Eg: Suppose there is an activity stack of A -> B -> C -> D. If we launch activity B again with the launch mode as “singleInstance”, the new activity stack will be:
Task1 — A -> B -> C
Task2 — D

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

How does the activity respond when the user rotates the screen?

A

When the screen is rotated, the current instance of activity is destroyed a new instance of the Activity is created in the new orientation. The onRestart() method is invoked first when a screen is rotated. The other lifecycle methods get invoked in the similar flow as they were when the activity was first created.

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

How to prevent the data from reloading and resetting when the screen is rotated?

A

The most basic approach would be to use a combination of ViewModels and onSaveInstanceState() . So how we do we that?
Basics of ViewModel: A ViewModel is LifeCycle-Aware. In other words, a ViewModel will not be destroyed if its owner is destroyed for a configuration change (e.g. rotation). The new instance of the owner will just re-connected to the existing ViewModel. So if you rotate an Activity three times, you have just created three different Activity instances, but you only have one ViewModel.
So the common practice is to store data in the ViewModel class (since it persists data during configuration changes) and use OnSaveInstanceState to store small amounts of UI data.
For instance, let’s say we have a search screen and the user has entered a query in the Edittext. This results in a list of items being displayed in the RecyclerView. Now if the screen is rotated, the ideal way to prevent resetting of data would be to store the list of search items in the ViewModel and the query text user has entered in the OnSaveInstanceState method of the activity.

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

Mention two ways to clear the back stack of Activities when a new Activity is called using intent

A

The first approach is to use a FLAG_ACTIVITY_CLEAR_TOP flag. The second way is by using FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in conjunction.

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

What’s the difference between FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP?

A

FLAG_ACTIVITY_CLEAR_TASK is used to clear all the activities from the task including any existing instances of the class invoked. The Activity launched by intent becomes the new root of the otherwise empty task list. This flag has to be used in conjunction with FLAG_ ACTIVITY_NEW_TASK.

FLAG_ACTIVITY_CLEAR_TOP on the other hand, if set and if an old instance of this Activity exists in the task list then barring that all the other activities are removed and that old activity becomes the root of the task list. Else if there’s no instance of that activity then a new instance of it is made the root of the task list. Using FLAG_ACTIVITY_NEW_TASK in conjunction is a good practice, though not necessary.

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