Activity Back Stack and Launch Mode Part -1

Raj Suvariya
WiseLTeach
Published in
5 min readApr 6, 2017

Have you ever wondered what happens to the current activity when you navigate to some other activity by taping some view on the screen? When you press back button how suddenly the same activity comes in font of you? If yes, then you are at the right place to get the answer about these kind of questions. In this blog we will learn about the activity back stack and launch mode of the activity. The blog is divided into 2 parts.

Knowledge before you read this blog
Every application has one or more tasks. Each task is differentiated by the task affinity which is unique for every task.

Application by default has only one task with task affinity equal to package name which can be found in androidmanifest.xml

To see, how many tasks a application has, you can just open recent-apps of your device and just count the number of instances of the same app, that is the number of tasks the app has.

Note: Generally, there will be only one task in your application, and which is the best way of writing your application. You should only create more than one task when it is exceptionally necessary. Like payment gateway page, which you want to keep unaffected by other tasks.

Activity Back Stack
The name itself suggest that it has something to do with stack of activities. As we discussed above every activity has either one or more tasks. For each task there is exactly one activity back stack. The topmost activity on the task’s back stack is visible on the screen. Usually, when the app launches the app has one task with just one activity (MainActivity) on it’s backstack.

Whenever, the user navigates from one activity to another the new activity is pushed on the top of the visible task’s back stack and it gets visible to the user, hiding the previous activity’s instance underneath. When user presses back the topmost activity is popped from the visible task’s backstack and the next activity in that stack gets visible on the screen.

This isn’t just limited to activities, even fragments can be pushed to the backstack. While navigating away from the fragment you can just add

fragment_transaction.addBackStack();

so when you press back the previous fragment gets visible on the screen.

Note: I have used “visible task” term in above discussion. What it means is that there might be lot of task but you can see only one on the screen so the one which is being showed to you is visible task.

Launch Modes
Again name itself suggest that it has something to do with how activity gets launched when we call startActivity(intent) or similar functions. There are basically four type of the launch modes.

  1. Standard
  2. SingleTop
  3. Single Task
  4. Single Instance

Standard

Definition as per Android Docs:
“The system always creates a new instance of the activity in the target task and routes the intent to it.”

Standard launch mode is default in all the activities you create in your android application. In this launch mode no matter what is there in activity back stack the new instance of the new activity gets created when we pass the intent of the new activity.

For example: Suppose you have very simple app with just one activity named MainActivity and only one button on the main activity. Now if you call start activity with intent of MainActivity on click of the button then it will launch the new instance of same activity again and the current instance will go to back stack. If you click the button 10 times then there will be 10 instances of the activity in the back stack. So you need to press back 10 times to get out of your application.

SingleTop

Definition as per Android Docs:
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

Which means that if we call startActivity with the intent of same activity which is on the top of back stack then the new instance will not be created rather the intent will be passed to activity via a method onNewIntent().

Taking the same example as above: Suppose you have very simple app with just one activity named MainActivity and only one button on the main activity. Now if the launch mode of the main activity is single top then if you click on the button which call startActivity with the intent of MainActivity then also it won’t launch new instance of the MainActivity because the instance on the top of the backstack is of MainActivity. So you can prevent users from opening new instance of same activity which is visible on the screen.

That’s all for part 1, To learn about SingleTask and SingleInstance checkout the part 2 of this blog.

Please mark ❤ if you like the blog. In case if you find any mistake please mention that in comment. Stay tuned for more tech blogs.

www.wiselteach.com

#AndroidMonk

#WiseL #WiseLTeach

--

--

Raj Suvariya
WiseLTeach

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