編輯:關於Android編程
andling affinities
處理affinity
The affinity indicates which task an activity prefers to belong to.
By default, all the activities from the same application have an affinity for each other.
So, by default, all activities in the same application prefer to be in the same task.
However, you can modify the default affinity for an activity.
Activities defined in different applications can share an affinity, or activities defined in the same application can be assigned different task affinities.
affinity表明了一個activity傾向屬於哪一個task
默認的,在同一個應用中所有的activity有相同的affinity
所以默認的,同一個應用中所有的activity傾向於在相同的task中。
然而,你可以修改activity的默認affinity
在不同應用中定義的activity可以共用同一個affinity,或者定義在同一個應用中的activity可以被指定不同的任務affinity
You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.
你可以通過<activity>元素的taskAffinity屬性修改任何已知的activity的affinity
The taskAffinity attribute takes a string value, which must be unique from the default package name declared in the <manifest> element, because the system uses that name to identify the default task affinity for the application.
taskAffinity屬性為一個與在<manifest>元素中默認包名不同的String類型,因為系統使用那個名字為應用標識默認任務affinity
The affinity comes into play in two circumstances:
affinity兩種情況下發揮作用
1.When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.
當啟動activity的intent包含FLAG_ACTIVITY_NEW_TASK標志。
A new activity is, by default, launched into the task of the activity that called startActivity().
It's pushed onto the same back stack as the caller.
However, if the intent passed to startActivity() contains the FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to house the new activity.
Often, it's a new task.
However, it doesn't have to be.
If there's already an existing task with the same affinity as the new activity, the activity is launched into that task.
If not, it begins a new task.
默認的,一個新activity會啟動到調用startActivity()的activity的task中。
它被推入與調用者相同的back stack中。
然而,如果傳遞給startActivity()的intent包含FLAG_ACTIVITY_NEW_TASK標志,系統會尋找一個不同task來儲存新activity。
經常是一個新的task
但是並不一定是這樣。
如果已經存在一個與新activity相同affinity的task,那麼這個activity會被啟動到此task中。
如果不是這樣,那就會啟動一個新的task。
If this flag causes an activity to begin a new task and the user presses the Home button to leave it, there must be some way for the user to navigate back to the task.
Some entities (such as the notification manager) always start activities in an external task, never as part of their own, so they always put FLAG_ACTIVITY_NEW_TASK in the intents they pass to startActivity().
If you have an activity that can be invoked by an external entity that might use this flag, take care that the user has a independent way to get back to the task that's started, such as with a launcher icon (the root activity of the task has a CATEGORY_LAUNCHER intent filter; see the Starting a task section below).
如果這個標志導致一個activity開啟一個新的task並且用戶按home鍵離開它,這裡必須給用戶提供一些方式可以導航回這個task。
一些實體(例如notification manager)總是在外部task打開activity,從來不作為他們自己的一部分,所以他們總是在intent中加入FLAG_ACTIVITY_NEW_TASK傳遞給startActivity()
******如果你有一個可以被可能含有此標識的外部實體調用的activity,注意用戶有一種不受約束的方式來返回到開始的task ,例如通過launcher圖標(task的根activity有一個CATEGORY_LAUNCHER的intent過濾器;參見下面的開啟一個 task章節)
2.When an activity has its allowTaskReparenting attribute set to "true".
當一個activity的allowTaskReparenting屬性設置為true
In this case, the activity can move from the task it starts to the task it has an affinity for, when that task comes to the foreground.
這種情況下,當那個task來到前台時,activity可以從它開始的task中移動到一個有為它的affinity的task中。
For example, suppose that an activity that reports weather conditions in selected cities is defined as part of a travel application.
It has the same affinity as other activities in the same application (the default application affinity) and it allows re-parenting with this attribute.
When one of your activities starts the weather reporter activity, it initially belongs to the same task as your activity.
However, when the travel application's task comes to the foreground, the weather reporter activity is reassigned to that task and displayed within it.
Tip: If an .apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application".
例如:假設一個選擇城市後報告天氣情況的activity被定義為一個旅游應用的一部分。
它與同應用中的其他activity擁有相同的affinity(應用默認的affinity),並且allowTaskReparenting=true。
當你的一個activity開啟天氣預報activity時,最初它與你的activity屬於同一個task。
但是,當這個旅游應用的task來到前台時,這個天氣預報activity被重新分配到此task中並且在其內部顯示。
提示:如果一個apk文件對於用戶來說包含多個“應用”,你也許想要使用taskAffinity屬性來分配不同的affinity到與activity關聯的每個“應用”中。
Clearing the back stack
清除back stack
If the user leaves a task for a long time, the system clears the task of all activities except the root activity.
When the user returns to the task again, only the root activity is restored.
The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.
如果用戶離開task很長時間,系統清理除了task中的根activity的其他所有activity
當用戶再次返回到task中,只有根activity被恢復。
系統之所以有這種行為是因為經過較長時間後,用戶很可能要放棄他們之前所做的,返回到task開始做些新的事情。
There are some activity attributes that you can use to modify this behavior:
一些activity屬性可以修改這種行為:
alwaysRetainTaskState
If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen.
The task retains all activities in its stack even after a long period.
如果task的根activity的這個屬性被設置為true,之前描述的默認行為就不會發生
即使在很長一段時間後,task也會保持在其stack內的所有activity
clearTaskOnLaunch
If this attribute is set to "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it.
In other words, it's the opposite of alwaysRetainTaskState.
The user always returns to the task in its initial state, even after a leaving the task for only a moment.
如果task的根activity的這個屬性被設置為true,每當用戶離開task再返回到它,stack都會被清除只留下根activity。
換句話說,它與alwaysRetainTaskState相反。
用戶總是返回到task的初始狀態,即使是只離開task一小會。
finishOnTaskLaunch
This attribute is like clearTaskOnLaunch, but it operates on a single activity, not an entire task.
It can also cause any activity to go away, including the root activity.
When it's set to "true", the activity remains part of the task only for the current session.
If the user leaves and then returns to the task, it is no longer present.
這個屬性和clearTaskOnLaunch相似,但是它操作單一的activity而不是整個task。
它也會導致activity離開,包括根activity。
當finishOnTaskLaunch設置為true時,activity只為當前會話保持部分task。
如果用戶離開又返回到這個task,這個activity將不復存在。
Starting a task
開始一個task
You can set up an activity as the entry point for a task by giving it an intent filter with "android.intent.action.MAIN" as the specified action and "android.intent.category.LAUNCHER" as the specified category. For example:
你可以通過指定一個action為android.intent.action.MAIN,category為android.intent.category.LAUNCHER的intent過濾器,設置一個activity作為task入口。
An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.
一個這種intent過濾器導致activity的一個圖標和標簽顯示在launcher應用中,給用戶一種方式來啟動activity和任何時候返回到activity被啟動之後它建立的task。
This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher.
For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter.
Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task.
The user then presses the Home button.
The task is now sent to the background and is not visible.
Now the user has no way to return to the task, because it is not represented in the application launcher.
第二點很重要:用戶必須有能力使用這個activity起動器離開task之後再返回到這個task。
由於這個原因,singleTask和singleInstance兩種啟動模式把activity標記成總是開始一個task,應該只被應用於當activity有ACTION_MAIN和CATEGORY_LAUNCHER過濾器時。
想一下,例如,如果沒有過濾器將會發生什麼:一個intent啟動一個啟動模式為singleTask的activity,初始化一個新的task,用戶花費一些時間在這個task上面
然後用戶按home鍵。
現在這個task被送到後台不再可見。
現在用戶沒有辦法返回到這個task上了,因為它沒有在launcher應用中表現。
For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true" (see Clearing the stack).
由於這些原因 你不希望用戶返回到activity的地方,設置<activity>元素的finishOnTaskLaunch屬性為true(見清除back stack)。www.2cto.com
效果圖點擊標簽,指示線滑動到當前的標簽下。 用到的技術: activity中添加 fragement, ViewPager, 自定義繪制view-Activity中動態添
As we all know,Android手機系統本質上是一個基於Linux的應用程序,它以Linux系統為內核。因此系統的啟動過程包括Linux內核啟動和Androi
一個Andoird應用程序的重要的地方是他們有相互溝通和整合的能力,一個應用程序可以和另一個應用程序交互,接下來我們來看看Android應用之間的內容分享當你構建Inte
最近很多人在問我,個人App開發者如何去設計UI。 其實這是個人開發者最頭痛的問題,搞技術的人,確實沒法做到面面俱到,不可能花大量的時間去切圖,去做原型設計,去做美工。