Have you ever come in a problem where you need to navigate across Activities in a very different way..! Or you want to skip activities when a user came after visiting many activities (you will never like to manage all those things by overriding onBackPressed().. Right? )
Choosing the perfect Android Activity Launch mode will make things done for you in a single LOC…
So, how will you select the appropriate Android launch mode for your need?
Let’s discuss all those four words in detail,
Before dive into this, what we need to understand is the Android Task. In android you can always navigate from one Activity to other.. (Yes.. Always.. Even if target Activity is not from your Application !) and, Android maintains this user experience by binding all those Activities inside a Task. User will back stacking in this task and move from one task to another.
Simply! Application is a Highway. The task is Lane on Highway. And, Activities are cars, driving on a particular lane of the highway. Cars can always change Lane ;-)
A very default launch mode! This will simply create a new Instance of Activity every time in a Target Task.
Very common Example, An Activity which is displaying a detailed description of an object. And you want it to create a new instance every time for different objects. Standard mode will create this for you.
Example: You have 5 Activities, let says, A, B, C, D, E all are defined as Standard
So, your stack can be like this: A-B-C-A-A-B-B
Another normal launch mode. If Activity’s launch mode is defined as a “SingleTop” and Activity is already on Top of target Task. It will simply route to the same Activity by onNewIntent() method otherwise, will behave normal and create a new instance.
So, As the name suggests. Activities can not have two or more consecutive instances on top of the task. But, can have multiple instances in the same or different tasks in a non-consecutive order.
Example: You have 5 Activities, let says, A, B, C, D, E. From there ‘B’ is defined as SingleTop
If there is no SingleTask activity instance is present in the system. This will simply behave normal and will create a new instance which will be on Top of the task.
Now, as per Short description. It can have only one instance throughout the system. So next time it will route to the same activity from any Task by onNewIntent() and will destroy all activities which are on top of SingleTask Activity to make our activity appear on top.
Example: You have 5 Activities, let says, A, B, C, D, E. From there ‘B’ is defined as SingleTask
Note: As per Android official documentation. It says SingleTask activity will create new Task and put our Activity on top of new task. However to achieve this behaviour taskAffinity concept need to be explored. They have described by assuming use of taskAffinity in a SingleTask activity.
SingleInstance is quite like SingleTask. The difference is, when first-time SingleInstance activity is created, it will create a new Task and put activity at the root of this newly created task. Again, this is a specialized task which will have only one activity as its member (A lane with only one car). That’s SingleInstance Vs. SingleTask.
If you again, call any default activity it will get out of this Task and create an instance in a previous task. And, every time you call this SingleInstance activity it will route to the same activity of separate task by onNewIntent().
Assume you have one ongoing call and you receives new call. What happen if newly received call just creates new instance of call screen ?
Use of single instance will never do this. It will route to the same Activity.
Example: You have 5 Activities, let says, A,B,C,D, E. From them ‘B’ is defined as SingleInstance
So, basically when you want multiple instances of activity, select Android Launch Mode from first two. And when you need your activity to be instantiated only one time throughout system. Use appropriate from last two.
SoluteLabs is a high-performance team of 25 focused on mobile and web design and development; we have produced top #10 chart-topping applications on Android and iOS app stores, graphics that have gone viral and applications with Millions of downloads.