Mar 132010
 

No matter how I read a bunch of words from the documentation about Activity Launch Modes, it just takes too damn long to understand. Here’s a table showing you what launch modes to use for activities you have.

Behavior Mode standard singleTop singleTask singleInstance
Can be launched by other activities? Y Y N N
Can have multiple instances? Y Y N N
Can launch other child activites? Y Y Y N
Reuse if activity is at the top of the stack? N Y N N

And here’s what kind of activity you should set for each mode:

Type of Activity Mode
Root Activity singleTask
One Activity Only singleInstance
Other Activity standard

Frankly, I don’t know when I will use singleTop mode. If anyone knows a type of activity that suits singleTop, please put a comment and tell me when you will use singleTop mode.

Here’s a common scenario though. What happens if you want to float an existing instance of Activity to the top of the stack? For example, your current stack is (bottom)A-B-C-D(top) and you want to float B to the top of the stack like this (bottom)A-C-D-B(top). You “start” the activity with an intent with the flag FLAG_ACTIVITY_REORDER_TO_FRONT like the following:

[sourcecode language="java"]
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
[/sourcecode]

I think that covers most of the usages of Activity Launch Modes for Android. Hopefully more Android titbits to come in future.