Manage Task based Android interview questions
Launch mode is an instruction for Android OS which specifies how the activity should be launched. It instructs how any new activity should be associated with the current task.
So in your scenario When using launchMode="singleInstance", there are two things that we need to remember:
- The Activity will always be created in a new task
- All Activities launched from this Activity will be created in a separate task
As such, an Activity with launchMode of singleInstance will always be isolated in its own task. There won't be another Activity inside of that task.
So with your example from your question of Activities A, B, C, and D:
- Activity A launches Activity B
- Activity B is launchMode="singleInstance" so it's on a new task
- Activity B launches Activity C
- Activity C is launched in the same task as Activity A
- Activity C launches Activity D
- Activity D is launchMode="singleInstance" so it's on a new task
From what happened here, you have one a task that stores the launchMode="standard" Activity A and Activity C. Activity B is in its own task. Activity D is in its own task.
Therefore, if you choose to Back out of these Activities, you'll notice that:
- Activity D is backed and Activity C appears
- Activity C is backed and Activity A appears
- This happens because Activity C is on the same task as Activity A.
Also, Activity D definitely won't be in the same task as Activity B because Activity B's task is meant only for Activity B due to launchMode="singleInstance".
Explain different launch modes in Android.
The different launch modes in Android are given below:
Standard:
- This launch mode generates an activity’s new instance in the task from which it originated.
- It is possible to create several instances for the same activity.
- For Example, suppose our current stack is A -> B -> C. Now, if we launch activity B again with the “standard” launch mode, then the new stack will be A -> B -> C -> B.
SingleTop:
- This launch mode is similar to the Standard launch mode except if there exists an activity’s previous instance on the top of the stack, then a new instance will not be created.
- But the intent will be sent to the activity’s existing instance.
- For example, suppose our current stack is A -> B -> C. Now, if we launch the activity B again with “singleTop” launch mode,then the new stack will be A -> B -> C -> B.
- Consider another example, where the current stack is A -> B -> C. Now, if we launch activity C again with the “singleTop” launch mode, then the stack will remain the same i.e., A -> B -> C. The intent will be passed to the onNewIntent() method.
SingleTask:
- This launch mode will create a new task and push a new instance to the task as the root.
- For example, suppose our current stack is A -> B -> C -> D. Now, if we launch activity B again with the “singleTask” launch mode, then the new stack will be A -> B. Here, a callback has been received on the old instance and C and D activities are destroyed.
SingleInstance:
- This launch mode is similar to the SingleTask launch mode. But the system doesn’t support launching any new activities in the same task.
- In a situation where the new activity is launched, it is launched in a separate task.
- For example, Suppose our current stack is A -> B -> C. Now, if we launch the activity D with the “singleInstance” launch mode, then there will be two stacks:
- A -> B -> C
- D, If you call activity E, then it will be added to the first stack.
- A -> B -> C -> E
- D
Again if you Call the activity D, then it will call the same activity from the 2nd stack and pass the intent to onNewIntent().
Aaaa
Comments
Post a Comment