flag是什麼

Android中發送Intent的時候有很多的標誌位可以使用。是在做各種各樣UI跳轉時,熟悉這些標誌的作用會給開發過程帶來很大的遍歷,這幾天找時間把這些Flag都看了一遍,順便翻譯了一下,爲便於理解,在翻譯的時候也加了一些說明性的東西,供自己以後參考用,順便分享出來與大家一起學習討論。

可能有些地方會有錯誤,歡迎大家指正,討論。

原創翻譯,如需轉載,請標明出處。

http://blog.csdn.net/javensun/article/details/8700265

其中有幾處地方還是存疑狀態,有問號和紅色標明,這幾天有空搞搞清楚。

FLAG_ACTIVITY_BROUGHT_TO_FRONT 默認標誌

This flag is not normally set by application code, but set for you by the system as described in the launchMode documentation for the singleTask mode.

通常在應用代碼中不需要設置這個FLAG,當launchMode爲singleTask時系統會默認設置這個標誌。

FLAG_ACTIVITY_CLEAR_TASK 清空任務標誌

If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

如果Intent中設置了這個標誌,會導致含有待啓動Activity的Task在Activity被啓動前清空。也就是說,這個Activity會成爲一個新的root,並且所有舊的activity都被finish掉。這個標誌只能與FLAG_ACTIVITY_NEW_TASK 一起使用。 

FLAG_ACTIVITY_CLEAR_TOP 清空任務中在其之上的Activity

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.

如果設置了這個標誌,並且待啓動的Activity已經存在於當前的task中,那就不會再給這個activity新起一個實例,而是將task中在它之上的其它activity全部關閉,然後把Intent作爲一個新的Intent傳給這個Activity(當前已在棧頂)。

例如,一個task中存在A,B,C,D四個Activity。如果D調用startActivity() 啓動B,那麼C和D會被finish掉並且B收到這個Intent,最後棧中只有A,B。

上面例子中運行的B activity既可以在onNewIntent()中接收新的Intent,也可以將自己finish掉然後使用新的Intent重啓。如果在它的launch mode中設置了"multiple"(默認),並且intent中沒有設置 FLAG_ACTIVITY_SINGLE_TOP 標誌,那它就會被finish掉然後重新創建。如果是其它的launchMode或者是設置了FLAG_ACTIVITY_SINGLE_TOP 屬性,那就會使用現有的實例的OnNewIntent()方法來接受Intent。

這種啓動模式也可以與 FLAG_ACTIVITY_NEW_TASK 一起使用:如果用來啓動一個任務的root activity,它會將這個任務中現在運行的實例調到前臺,然後將任務清空至只有根Activity的狀態。這很有用,例如要從通知中心裏啓動一個Activity時。

FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 任務重置時將任務中在此標記之後的Activity清空

If set, this marks a point in the task's activity stack that should be cleared when the task is reset. That is, the next time the task is brought to the foreground with FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (typically as a result of the user re-launching it from home), this activity and all on top of it will be finished so that the user does not return to them, but instead returns to whatever activity preceeded it.

This is useful for cases where you have a logical break in your application. For example, an e-mail application may have a command to view an attachment, which launches an image view activity to display it. This activity should be part of the e-mail application's task, since it is a part of the task the user is involved in. However, if the user leaves that task, and later selects the e-mail app from home, we may like them to return to the conversation they were viewing, not the picture attachment, since that is confusing. By setting this flag when launching the image viewer, that viewer and any activities it starts will be removed the next time the user returns to mail.

設置這個標誌意味着在activity棧中做一個標記,在Task重置的時候棧就把從標記往上的activity都清除。也就是說,下次這個Task被通過FLAG_ACTIVITY_RESET_TASK_IF_NEEDED調到前臺時(通常是由於用戶從桌面重新啓動),這個activity和它之上的activity都會被finish掉,這樣用戶就不會再回到他們,而是直接回到在它們之前的activity。

這在應用切換時非常有用。比如,Email應用會需要查看附件,就要調用查看圖片的Activity來顯示,那這個查看圖片的Activity就會成爲Email應用任務裏的一部分。但是,如果用戶離開了Email的任務,過了一會兒由通過Home來選擇Email應用,我們會希望它回到查看郵件會話的頁面,而不是瀏覽圖片附件的頁面,不然就感覺太詭異了。如果在啓動查看圖片Activity時設置了這個標誌,那這個Activity及由它啓動的Activity在下一次用戶返回郵件時都會被清除。

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 不顯示在近期任務中

If set, the new activity is not kept in the list of recently launched activities.

如果設置這個標誌,這個Activity就不會在近期任務中顯示。

FLAG_ACTIVITY_FORWARD_RESULT 轉發結果

If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.

如果Activity A 在啓動 Activity B時設置了這個標誌,那A的答覆目標目標會傳遞給B,這樣一來B就可以通過調用setResult(int) 將返回結果返回給A的答覆目標。

簡單如下:

O ----startActivityForResult()----> A ----FLAG_ACTIVITY_FORWARD_RESULT----> B

A的答覆目標是O,如果A在啓動B時使用了這個標誌,A就會把答覆目標O的信息傳遞給B,以便B將O作爲它的答覆目標。此時B調用setResult()時的結果信息都會傳遞給O,而不會給A。並且此時在A中調用setResult()的內容不會生效。我還沒發現使A中setResult()生效的方法。

注意:這個標誌不能與startActivityForResult()一起使用。

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 從近期任務中啓動的標誌

This flag is not normally set by application code, but set for you by the system if this activity is being launched from history (longpress home key).

這個標誌通常情況下不會通過應用的代碼來設置,而是在通過最近任務啓動activity時由系統設置的。

FLAG_ACTIVITY_MULTIPLE_TASK Activity可在多任務運行的標誌

Do not use this flag unless you are implementing your own top-level application launcher. Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.

Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.

This flag is ignored if FLAG_ACTIVITY_NEW_TASK is not set.

See Tasks and Back Stack for more information about tasks.

除非你實現了自己的頂級應用啓動器,否則不要使用這個標誌。與 FLAG_ACTIVITY_NEW_TASK 一起使用可以不再把已存在的任務喚起到前臺。 當被設置時,系統總會爲Intent的Activity啓動一個新的task,而不管是否已經有已存在的任務在做同樣的事情。

因爲默認系統不包含圖形化的任務管理功能,所以除非你給用戶提供了返回到已啓動任務的方法,否則就不要用這個標誌。

如果FLAG_ACTIVITY_NEW_TASK沒有設置,則這個標誌也被忽略。

FLAG_ACTIVITY_NEW_TASK 嘗試在新任務中啓動Activity的標誌(並不一定就會在新的任務中)

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.

設置這個標誌可以爲待啓動的Activity創建一個新的任務。一個任務(從啓動它的Activity到任務中的下一個Activity)就是用戶可以跳轉到的Activity的原子羣。任務可以在前臺與後臺之間切換;在某一特定任務之中的所有Activity一直會保持同樣的順序。

這個標誌通常被用來呈現一種"laucher"類型的行爲:爲用戶提供一個可單獨解決的事情列表,完全獨立於啓動他們的Activity之外運行。

使用這個標誌時,如果有一個任務已經運行了你要啓動的Activity,那就不會在創建新的Activity,而是將現有的任務保持之前的狀態直接喚到前臺。參見FLAG_ACTIVITY_MULTIPLE_TASK這個標誌,可以禁用掉這個行爲。

這個標誌不能在調用者向待啓動Activity請求返回結果時使用。

注意:假設A啓動B,如果要讓B在新的task中創建,要求這兩個Activity的taskAffinity不同。也就是說,設置了這個標誌後,新啓動的activity並非就一定在新的task中創建,如果A和B在屬於同一個package,而且都是使用默認的taskAffinity,那B還是會在A的task中被創建。 所以,只有A和B的taskAffinity不同時,設置了這個標誌纔會使B被創建到新的task。

FLAG_ACTIVITY_NO_ANIMATION 禁用切換動畫

If set in an Intent passed to Context.startActivity(), this flag will prevent the system from applying an activity transition animation to go to the next activity state. This doesn't mean an animation will never run -- if another activity change happens that doesn't specify this flag before the activity started here is displayed, then that transition will be used. This flag can be put to good use when you are going to do a series of activity operations but the animation seen by the user shouldn't be driven by the first activity change but rather a later one.

禁用掉系統默認的Activity切換動畫。

FLAG_ACTIVITY_NO_HISTORY 不保存Activity的歷史狀態

If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.

如果設置這個標誌,新的Activity就不會在歷史棧中保存。用戶一旦離開,這個Activity就會finish掉。也可以使用noHistory屬性設置。

FLAG_ACTIVITY_NO_USER_ACTION 不響應onUserLeaveHint方法

If set, this flag will prevent the normal onUserLeaveHint() callback from occurring on the current frontmost activity before it is paused as the newly-started activity is brought to the front.

Typically, an activity can rely on that callback to indicate that an explicit user action has caused their activity to be moved out of the foreground. The callback marks an appropriate point in the activity's lifecycle for it to dismiss any notifications that it intends to display "until the user has seen them," such as a blinking LED.

If an activity is ever started via any non-user-driven events such as phone-call receipt or an alarm handler, this flag should be passed to Context.startActivity, ensuring that the pausing activity does not think the user has acknowledged its notification.

如果設置了這個標誌,可以在避免用戶離開當前Activity時回調到 onUserLeaveHint(). 通常,Activity可以通過這個回調錶明有明確的用戶行爲將當前activity切出前臺。 這個回調標記了activity生命週期中的一個恰當的點,可以用來“在用戶看過通知之後”將它們清除,如閃爍LED燈。

如果Activity是由非用戶驅動的事件(如電話呼入或鬧鐘響鈴)啓動的,那這個標誌就應該被傳入Context.startActivity,以確保被打斷的activity不會認爲用戶已經看過了通知。

FLAG_ACTIVITY_PREVIOUS_IS_TOP

If set and this intent is being used to launch a new activity from an existing one, the current activity will not be counted as the top activity for deciding whether the new intent should be delivered to the top instead of starting a new one. The previous activity will be used as the top, with the assumption being that the current activity will finish itself immediately.

如果啓動Activity時設置了這個標誌,那當前這個 Activity 不會被當作頂部的 Activity 來判斷是否之後新Intent應該被傳給棧頂Activity而不是啓動一個新的Activity。之前一個的Activity會被當作棧頂,假定當前的Acitvity會立即自己finish掉。

即 A---> B --->C,若B啓動C時用了這個標誌位,那在啓動時B並不會被當作棧頂的Activity,而是用A做棧頂來啓動C。此過程中B充當一個跳轉頁面。

典型的場景是在應用選擇頁面,如果在文本中點擊一個網址要跳轉到瀏覽器,而系統中又裝了不止一個瀏覽器應用,此時會彈出應用選擇頁面。在應用選擇頁面選擇某一款瀏覽器啓動時,就會用到這個Flag。然後應用選擇頁面將自己finish,以保證從瀏覽器返回時不會在回到選擇頁面。

經常與FLAG_ACTIVITY_FORWARD_RESULT 一起使用。

FLAG_ACTIVITY_REORDER_TO_FRONT 任務中的Activity順序重排

If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.

For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.

如果設置了這個標誌,而且被啓動的Activity如果已經在運行,那這個Activity會被調到棧頂。

比如,一個任務中有4個Activity:A,B,C,D。如果D調用了startActivity() 來啓動B時使用了這個標誌,那B就會被調到歷史棧的棧頂,結果順序:A,C,D,B,否則順序會是:A,B,C,D,B。 如果使用了標誌 FLAG_ACTIVITY_CLEAR_TOP,那這個FLAG_ACTIVITY_REORDER_TO_FRONT標誌會被忽略。

==============================================================================================================================

FLAG_ACTIVITY_RESET_TASK_IF_NEEDED???

If set, and this activity is either being started in a new task or bringing to the top an existing task, then it will be launched as the front door of the task. This will result in the application of any affinities needed to have that task in the proper state (either moving activities to or from it), or simply resetting that task to its initial state if needed.

如果設置,

FLAG_ACTIVITY_SINGLE_TOP 

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

設置這個標誌之後,如果被啓動的Activity已經在棧頂,那它就不會被再次啓動。

FLAG_ACTIVITY_TASK_ON_HOME 直接返回桌面

If set in an Intent passed to Context.startActivity(), this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one). That is, pressing back from the task will always return the user to home even if that was not the last activity they saw. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

這個標誌可以將一個新啓動的任務置於當前的home任務(home activity task)之上(如果有的話)。也就是說,在任務中按back鍵總是會回到home界面,而不是回到他們之前看到的activity。這個標誌只能與FLAG_ACTIVITY_NEW_TASK標誌一起用。

比如,A->B->C->D,如果在C啓動D的時候設置了這個標誌,那在D中按Back鍵則是直接回到桌面,而不是C。

注意:

只有D是在新的task中被創建時(也就是D的launchMode是singleInstance時,或者是給D指定了與C不同的taskAffinity並且加了FLAG_ACTIVITY_NEW_TASK標誌時),使用 FLAG_ACTIVITY_TASK_ON_HOME標誌纔會生效。

感覺實際使用效果和用 FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK 的效果一樣。

FLAG_DEBUG_LOG_RESOLUTION 打開Activity解析的Log開關

A flag you can enable for debugging: when set, log messages will be printed during the resolution of this intent to show you what has been found to create the final resolved list.

可以用來啓用調試的標誌:設置以後,在intent的處理過程中log信息會打印出來,可以看到都找到了些什麼來創建最終的解析列表。

Log輸出如下:

04-15 11:19:09.446: V/IntentResolver(398): Resolving type null scheme null of intent Intent { act=android.intent.action.B_Activity flg=0x8 }
04-15 11:19:09.446: V/IntentResolver(398): Action list: [ActivityIntentInfo{42354338 com.example.icsorigintest.B_Activity}]
04-15 11:19:09.446: V/IntentResolver(398): Matching against filter ActivityIntentInfo{42354338 com.example.icsorigintest.B_Activity}
04-15 11:19:09.446: V/IntentResolver(398):   Filter matched!  match=0x108000
04-15 11:19:09.446: V/IntentResolver(398): Final result list:
04-15 11:19:09.446: V/IntentResolver(398):   ResolveInfo{41ee95d0 com.example.icsorigintest.B_Activity p=0 o=0 m=0x108000}

FLAG_EXCLUDE_STOPPED_PACKAGES 排除已停止的包

If set, this intent will not match any components in packages that are currently stopped. If this is not set, then the default behavior is to include such applications in the result.

設置之後,Intent就不會再匹配那些當前被停止的包裏的組件。如果沒有設置,默認的匹配行爲會包含這些被停止的包。

FLAG_FROM_BACKGROUND 後臺啓動Activity

Can be set by the caller to indicate that this Intent is coming from a background operation, not from direct user interaction.

可以給調用者用來標識這個Intent是來自後臺操作,而不是用戶的交互行爲。

FLAG_GRANT_READ_URI_PERMISSION 給Activity授權

If set, the recipient of this Intent will be granted permission to perform read operations on the Uri in the Intent's data and any URIs specified in its ClipData.When applying to an Intent's ClipData, all URIs as well as recursive traversals through data or other ClipData in Intent items will be granted; only the grant flags of the top-level Intent are used.

如果設置,Intent的接收者會被授予讀權限,用來讀取Intent中包含的或是在ClipData中指定的Uri。當被用於Intent中的ClipData時,被授予的是Intent中其它ClipData中的所有Uri和它們遞歸遍歷到的Uri的讀權限。只有頂級Intent中的授予標誌會被使用。

FLAG_INCLUDE_STOPPED_PACKAGES 包含已停止的包

If set, this intent will always match any components in packages that are currently stopped. This is the default behavior when FLAG_EXCLUDE_STOPPED_PACKAGES is not set. If both of these flags are set, this one wins (it allows overriding of exclude for places where the framework may automatically set the exclude flag).

設置之後Intent總是會去匹配那些已被停止的包裏的組件。如果沒有設置 FLAG_EXCLUDE_STOPPED_PACKAGES 標誌,那這個就是默認行爲。如果兩個標誌都被設置,那這個會生效(在框架中一些地方可能會自動設置exclude標誌,這些標誌可以被覆蓋掉)。

FLAG_RECEIVER_FOREGROUND 接受器以前臺優先級運行

If set, when sending a broadcast the recipient is allowed to run at foreground priority, with a shorter timeout interval. During normal broadcasts the receivers are not automatically hoisted out of the background priority class.

當發送廣播的時候設置了這個標誌,會允許接收者以前臺的優先級運行,有更短的時間間隔。正常廣播的接受者是後臺優先級,不會被自動提升。

FLAG_RECEIVER_REGISTERED_ONLY 只調用手動註冊的接收器(忽略manifest中聲明的)

If set, when sending a broadcast only registered receivers will be called -- no BroadcastReceiver components will be launched.

如果發送廣播時設置了這個標誌,那隻會調用註冊了的接收器——BroadcastReceiver組件不會被啓動。

FLAG_RECEIVER_REPLACE_PENDING 替換掉等待中的廣播

If set, when sending a broadcast the new broadcast will replace any existing pending broadcast that matches it. Matching is defined by Intent.filterEquals returning true for the intents of the two broadcasts. When a match is found, the new broadcast (and receivers associated with it) will replace the existing one in the pending broadcast list, remaining at the same position in the list.

This flag is most typically used with sticky broadcasts, which only care about delivering the most recent values of the broadcast to their receivers.

如果在發送廣播時設置了這個標誌,那新的廣播會替換掉那些已存在的相同廣播。相同的定義是通過Intent.filterEquals方法對兩個廣播的Intent處理返回true。 當匹配到相同的,新的廣播和對應的接收器會將待發送的廣播列表中已存在的替換掉,在列表中保留同樣的位置。

這個標誌通常被粘性廣播(Sticky Broadcast)使用,只保證將最新的廣播的值傳遞給接收器。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章