I’ve written a post about Android back stack and its default behaviour where every started activity is placed in a “last in, first out” stack. Today, I was reading a great article on Android guides where there is a section to explain about situations where we must to tweak this behavior, something that I forgot to mention on my posts.
- Have an activity in your application to begin a new task when it is started.
- Want to bring forward an existing instance of an activity (rather than create a new one).
- Want to clear the history of the back stack at a particular point.
How can we solve this situations only with standard behavior? We can’t. We need to change the default behavior with others activity launch modes and we have two approaches to do it. First one is thought intent flags “
intent.addFlags(Intent.A_FLAG_HERE);” or activity configuration specified on Android Manifest file “
There are three launch modes that we can use, besides the standard.
- standard – The default behavior. Creates a new instance of the activity in the task.
- singleTop – Reuse an activity instance if already at the top of the stack; otherwise create new instance.
- singleTask – Reuse an activity instance if exists in an existing stack; otherwise create in a new task stack.
- singleInstance – Same as singleTask but no other activities are ever inserted into the created task stack.
And flags…we have a lot of them…=D We can see a great post about each one here.
There is an additional activity property called noHistory “
android:noHistory="true"“. Once this is set, that activity will never be a part of the stack even when launched.