singleTask launchMode in Android
- Authors
- Name
- Amit Shekhar
- Published on
I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
Join Outcome School and get high paying tech job: Outcome School
Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
In this blog, we will learn about the singleTask
launchMode in Android.
We use launchMode
to give instructions to the Android operating system about how to launch the activity.
There are various types of launchMode
in Android, but for this blog, we are focusing on singleTask
.
singleTask launchMode
When we use launchMode="singleTask"
, the following cases are possible:
- If an Activity does not exist in an already created Task, then it starts the new Activity in a new Task with the Activity's new instance at the root of that Task.
- If an Activity exists in an already created Task, the Task is brought forward with the Activity's last state restored and this Activity receives the new intent through the
onNewIntent()
method. - Only one instance of the Activity can exist at a time.
Now, let's understand the above points with examples.
Assume we have the Activities as A, B, C, D, and E. And, we have added launchMode="singleTask"
for Activity C.
Example 1
Let's say we have the state of the Activity Stack before launching Activity C as follows:
A > B
Here, we navigated from Activity A to Activity B. And now we want to navigate to Activity C from Activity B.
Keep this in mind that we have launchMode="singleTask"
for Activity C.
Now, after the launch of Activity C from Activity B, the state of the Activity Stack will become as follows:
A > B > C
Here, Activity C gets launched as usual.
Example 2
Let's say we have the state of the Activity Stack before launching Activity C as follows:
A > B > C > D > E
Here, we navigated from Activity A to Activity B to Activity C to Activity D to Activity E. And now we want to navigate to Activity C from Activity E.
Keep this in mind that we have launchMode="singleTask"
for Activity C.
Now, after the launch of Activity C from Activity E, the state of the Activity Stack will become as follows:
A > B > C
Two important things to note here:
- Here, Activity C is the old instance only that gets the extras data through
onNewIntent()
. - Activities D and E get destroyed.
Now, we have understood about the singleTask
launchMode in Android.
Watch the video format: singleTask launchMode in Android
Prepare yourself for Android Interview: Android Interview Questions
That's it for now.
Thanks
Amit Shekhar
Co-Founder @ Outcome School
You can connect with me on:
Follow Outcome School on: