1.Demo說明與示範
該Demo主要示範FLAG_ACTIVITY_REORDER_TO_FRONT的使用
如下:
2.視頻講解
http://www.eyeandroid.com/thread-10717-1-1.html
3.Demo分析
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);
此時棧如所示: