Reorder Activities 示例有四個相關的Activitives: ReorderOnLaunch, ReorderTwo,ReorderThree, ReorderFour。其中ReorderOnLaunch為主Activity,ReorderOnLaunch啟動ReorderTwo ,ReorderTwo啟動 ReorderThree,ReorderThree啟動 ReorderFour。 這時在Activity的”back stack”有如下狀態:
ReorderFour 想再啟動ReorderTwo, 這時用兩種實現方法,一是在 RecordFour之上再啟動一個新的 ReorderTwo ,這是startActivity的缺省行為。這是因為在AndroidManifest.xml:
<activity android:name=”.app.ReorderTwo” /> <activity android:name=”.app.ReorderThree” /> <activity android:name=”.app.ReorderFour” />
?
.app.RecordTwo沒有定義任何這個Intent的Flag。 比如有
FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP,在這些情況下 Android將根據不同Flag設置來決定啟動RecordTwo的方法,具體在后面的例子會有介紹。
在Reorder Activities例子中,是將“Back Stack”中的ReorderTwo移到棧頂,方法是在啟動Intent時設置 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT。如果設置了 FLAG_ACTIVITY_REORDER_TO_FRONT表示將“Back Stack”中指定的Activity移動到棧頂。
Intent intent = new Intent(ReorderFour.this, ReorderTwo.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent);
?
此時棧如下圖所示:
?