Intent flag 與啟動模式的對應關系


Activity有四種啟動模式:

1.standard(標准)    2.singleTop    3.singleTask  4.singleInstance

標識某個Activity的啟動模式,有兩種方式:

1.一種是通過AndroidManifest.xml    2.一種是通過Intent的標識

通過AndroidManifest.xml來標識:

  1. <activity android:name=".Activity1"
  2. android:launchMode="standard"
  3. android:label="@string/app_name">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>

通過Intent的Flag來標識:

FLAG_ACTIVITY_NEW_TASK

FLAG_ACTIVITY_SINGLE_TOP

FLAG_ACTIVITY_CLEAR_TOP

------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_NEW_TASK---沒有對應的啟動模式,它的部分特征與singleTask類似。

public static final int FLAG_ACTIVITY_NEW_TASK

Added in API level 1

If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order. See Tasks and Back Stack for more information about tasks.

This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from the activity being launched.

Constant Value: 268435456 (0x10000000)

使用這個特征的Intent,系統會為被啟動的Activity創建一個新的task,然后被啟動的Activity存放在回退棧的底部。

實驗:

1.在同一個應用中:

Image(11)

在啟動Acitivyt時,設定FLAG_ACTIVITY_NEW_TASK標識

Intent newTask = new Intent(MainActivity.this ,com.intentflag.activity.ActivityNewTaskFlag.class);

newTask.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                                  startActivity( newTask);

效果如圖:

Image(12)

由上兩個圖,可以發現,它們的taskId都是一樣的,這說明,使用FLAG_ACTIVITY_NEW_TASK標識時,

在同一個應用中,並沒有上述描述的效果,會啟動一個新的TASK來存放被啟動的Activity實例。

Image(13)

Image(14)

從times值得變化來看,如果當前task中,有被啟動Activity的實例,那么,即使使用了FLAG_ACTIVITY_NEW_TASK標識,系統依然會創建一個新的實例。

2.在兩個不同的應用中:

1).在使用FLAG_ACTIVITY_NEW_TASK標識的情況下

這是被啟動Activity所在的應用:

Image

這是啟動者所在的應用:

Image

成功啟動后:

Image

然后,再按下返回鍵:

Image

可以知道:1.被啟動的Activity是與啟動者處於不同的task中的。2.系統會創建一個新的被啟動Activity實例。

3.新的Activity是處在一個新的task的頂部,如果該task已經存在的話,則該Activity實例,位於棧頂。

2).在不使用FLAG_ACTIVITY_NEW_TASK的情況下:

Image

Image

從結果可以知道,如果不使用FLAG_ACTIVITY_NEW_TASK,那么,被啟動的Activity將會與啟動者使用同一個task,是新創建一個Activity,然后,將該Activity實例存放到與啟動者使用的task中。

總結,FLAG_ACTIVITY_NEW_TASK,適合,啟動哪些與被啟動者不是同一個應用的Activity。一個應用,用一個task來組織它的所有Activity。

--------------------------------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_SINGLE_TOP

public static final int FLAG_ACTIVITY_SINGLE_TOP

Added in API level 1

If set, the activity will not be launched if it is already running at the top of the history stack.

Constant Value: 536870912 (0x20000000)

使用這個標識去

例子,在兩個應用之間來使用:

被啟動Activity,在另外一個應用中是處於棧頂的:

Image

然后,在另外一個應用中,使用FLAG_ACTIVITY_SINGLE_TOP標識來啟動:

Image

然后,啟動之后的結果是:

Image

經過實驗發現,並沒有上述宣稱的效果。

可以知道,是創建了一個新的Cactivity。

---------------------------------------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_CLEAR_TOP

public static final int FLAG_ACTIVITY_CLEAR_TOP

Added in API level 1

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

See Tasks and Back Stack for more information about tasks.

Constant Value: 67108864 (0x04000000)

例子:

在默認情況下,只使用了FLAG_ACTIVITY_CLEAR_TOP標識

根據文檔的描述,A->B->C->D,要啟動B這個Activity,然后,使用的標識是FLAG_ACTIVITY_CLEAR_TOP,

然后,回退棧中的其它Activity會被清除掉,並且,B也會被銷毀掉,然后重新創建。

最后的結果是:A-->B(新創建的)

實驗:

Image

Image

Image

Image

Image

然后,按下返回鍵,結果是返回到A,這說明,B,C,D都被清除掉了,棧頂是新的B。

Image

這個效果,與文檔所說明的效果是相同的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM