Android启动模式

Android: Understanding Activity launch mode

  • ‘standard’ and ‘singleTop’ can instantiate multiple activity instances and the instance will stay in the same task.
  • For ‘singleTask’ or ‘singleInstance’, the activity class uses the singleton pattern, and that instance will be the root activity of a new task. Let’s examine each value:

the weird launch mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
https://stackoverflow.com/questions/9363200/android-singletask-singletop-and-home-button
The behaviour of activity back stack becomes quit weird when define main activity with singleTask at the same time:

<activity android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

What even worse is there is no clear explanation in the official dev guide regarding to this special use case. Some sections related to this topic are even self-contradictory.

Try using launchMode="standard" on your MainActivity A, and launchMode="singleTask" on your Activity B, which will give the expect behaviour you described.

自学笔记:Activity的启动模式:FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT

1
2
3
4
5
6
7
// A, B, C 和 D ==> A, B
Intent intent = new Intent(this, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

// add below code , B will be reused.
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
1
2
3
4
// A,B,C和D ==> A,C,D,B,
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

深入讲解Android中Activity launchMode - 技术小黑屋