Android應用程序組件Activity的"singleTask"(9)
這個函數中作用無非就是找到ID等于參數taskId的任務,然后在這個任務中查找是否已經存在即將要啟動的Activity的實例。
如果存在,就會把這 個Actvity實例上面直到任務堆棧頂端的Activity通過調用finishActivityLocked函數將它們結束掉。
在這個例子中,就是要 在屬性值affinity等于"shy.luo.task"的任務中看看是否存在SubActivity類型的實例,如果有,就把它上面的 Activity都結束掉。
這里,屬性值affinity等于"shy.luo.task"的任務只有一個MainActivity,而且它不是 SubActivity的實例,所以這個函數就返回null了。
回到前面的startActivityUncheckedLocked函數中,這里的變量top就為null了,于是執行下面的else語句:
- [java] view plaincopy if (top != null) {
- ......
- } else {
- // A special case: we need to
- // start the activity because it is not currently
- // running, and the caller has asked to clear the
- // current task to have this activity at the top.
- addingToTask = true;
- // Now pretend like this activity is being started
- // by the top of its task, so it is put in the
- // right place.
- sourceRecord = taskTop;
- }
于是,變量addingToTask值就為true了,同時將變量sourceRecord的值設置為taskTop,即前面調用findTaskLocked函數的返回值,這里,它就是表示MainActivity了。
繼續往下看,下面這個if語句:
- [java] view plaincopy if (r.packageName != null) {
- // If the activity being launched is the same as the one currently
- // at the top, then we need to check if it should only be launched
- // once.
- ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
- if (top != null && r.resultTo == null) {
- if (top.realActivity.equals(r.realActivity)) {
- if (top.app != null && top.app.thread != null) {
- ......
- }
- }
- }
- } else {
- ......
- }


















