Activity Back Stack and Launch Mode Part -2

Raj Suvariya
WiseLTeach
Published in
3 min readApr 18, 2017

In the first part we learned about activity backstack and two launch modes of Activity. If you haven’t read the first part then do it now and come back when you are done.

In this second part we will learn about two more Activity launchmodes : SingleTask and SingleInstance.

SingleTask

Definition as per Android Docs:
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.”

What does it mean is only one instance of any activity can exist through all the tasks of the application. If there is already one instance sitting on back stack of any task inside the application and we call startActivity() then all the activities above that instance from the particular task’s backstack will be removed and that task and the activity instance will become visible on the screen.

For ex.
Suppose, there are 4 activities A,B,C and D. C has launchmode SingleTask and the present application backstack is shown at top of the image. Now if we call startActivity(c) then the backstack would became like shown at bottom of the image.

Note: In the Android docs definition it is mentioned “The system creates the activity at the root of a new task and routes the intent to it”. However this holds only true when the activity having launchmode as SingleTask also has different taskAffinity then application. If it has the taskAffinity matching any other task inside the application then the new instance will be pushed on the top of the back stack as usual.

You might have question that “How to change the content from the previous instance?”. Well, you can override the onNewIntent() method of the Activity to get new intent and update the information on the screen.

SingleInstance

Definition as per Android Docs:
Same as “singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task”.

It’s pretty clear from the definition itself that it is just like the SingleTask except the activity will be created as the root of the new task regardless of the taskAffinity and that task cannot contain any more activity instance. So the task will contain only one activity instance with launchmode as SingleInstance.

If we navigate to any other activity from the SingleInstance activity it takes us back to previous task and performs the new activity creation as per it’s launch mode on that task.

Also, in the case of SingleInstance even if there are more than one task we can only see one instance in recent apps.

Here is the comparison of all the launchmodes.

If you want to play around by changing the launchmodes of the activities you can get the starter code here.

If you like the blog please mark ❤ and also share it with your fellow developer. Also subscribe to our newsletter to get notifications about ourupcoming blogs.

www.wiselteach.com

#AndroidMonk

#WiseL #WiseLTeach

--

--

Raj Suvariya
WiseLTeach

Software Development Engineer @Flipkart. Ex-InMobi, Ex-Tekion