Android Activity Launch Mode

When we launch any activity, we have to decide how we want to open this activity, 
that is called the mode of the activity, some developers will call it android launch mode and some will prefer it as activity launch mode.
don't confuse as both are the same.



come to the point, as we all know most of the time we have not mentioned any launch mode, but our activity launches successfully, how?
There is one default launch mode of the activity if we had not mentioned any,
the default one is applied to that activity. 

Android has a 4 launch mode for the activity.

  1. standard
  2. singleTop
  3. singleTask
  4. singleInstance


Standard:-

Standard mode is the default Launch method of activity. incase you are not setting any launch mode in your activity, it will utilize the standard mode naturally.it does not matter that activity is already open or not, this launch mode always create a new instance

Assume we have A, B, C, and D activity and your activity B has default standard mode. Presently again launching activity B 

State of Activity Stack before launch B

A →B→ C→D

State of Activity Stack after launch B

A → B → C→D→ B

We can see that a new instance of B is made once more.

SingleTop:-

If the activity instance is already created and existed at the topmost position of the current activity stack, in this case, no new instance will be created, our Android system will pass the information using onNewIntent() method.

The new instance will be created only when the instance is not presented at the topmost position 

Assume we have A, B, C, and D activities. A →B →C →D 

suppose now we launch C again, a new instance of C will be created because C is not on the topmost. 

So it will resemble A →B →C →D →C 

Presently assume we have A →B →C →D →C like this 

at that point we on the off chance that again activity C launches, at that point for this situation new occurrence won't be made. Rather, we will get the callback on onNewIntent() method.

SingleTask:-
activity having singleTask launch mode have only one instance in the stack it can be called singleton.
singleTask launch mode means only one instance will exist in the system. 

if the launching activity instance is absent, at that point the new instance of that activity will be made, and the instance is as of now present in the stack, at that point the onNewIntent() method will get the callback. 

Assume we have A, B, C activities(A →B →C ) and we are launching D that has a singleTask launch mode. All things considered, the new instance of D will be made so the present state will resemble this. (A →B →C →D) 

Presently let assume that we launch B that additionally have has a singleTask launch mode, at that point the current state will resemble this. 

A →B 

Here old instance gets called and intent information course through onNewIntent() callback. 
and the most important fact of this launch mode notes that C and D activity get destroy here.

SingleInstance:-
It is like singleTask with the exception that no different activities will be made in a similar task. In the event that another Activity is called from this sort of Activity, another Task would be naturally made to put that new Activity. 

Case 1: 

Assume you have A, B, and C activities(A →B →C) and your activity D has a singleInstance launch mode. For this situation, if we launch D, at that point D will be launch in a different task. A new task for D will be made. 

Task1: A →B →C 

Task2 : D (here D will be in another task) 

Presently if you proceed with this and start E and D, at that point Stack will resemble 

Task1: A →B →C →E 

Task2: D 

Case 2: 

Assume you have A, B, C activities in a single task (A →B →C), and movement D is in another task with launch mode singleInstance. For this situation, in the event that we launch D once more, at that point we will get the callback in the onNewIntent() technique for D action. 

Task1: A →B →C 

Task2: D (Here old occurrence gets called and information route through onNewIntent() callback)

thanks for being here
#stayHome #staySafe #stayCode

No comments:

Post a Comment