Android應(yīng)用程序組件Activity的"singleTask"(9)
這個(gè)函數(shù)中作用無非就是找到ID等于參數(shù)taskId的任務(wù),然后在這個(gè)任務(wù)中查找是否已經(jīng)存在即將要啟動(dòng)的Activity的實(shí)例。
如果存在,就會(huì)把這 個(gè)Actvity實(shí)例上面直到任務(wù)堆棧頂端的Activity通過調(diào)用finishActivityLocked函數(shù)將它們結(jié)束掉。
在這個(gè)例子中,就是要 在屬性值affinity等于"shy.luo.task"的任務(wù)中看看是否存在SubActivity類型的實(shí)例,如果有,就把它上面的 Activity都結(jié)束掉。
這里,屬性值affinity等于"shy.luo.task"的任務(wù)只有一個(gè)MainActivity,而且它不是 SubActivity的實(shí)例,所以這個(gè)函數(shù)就返回null了。
回到前面的startActivityUncheckedLocked函數(shù)中,這里的變量top就為null了,于是執(zhí)行下面的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了,同時(shí)將變量sourceRecord的值設(shè)置為taskTop,即前面調(diào)用findTaskLocked函數(shù)的返回值,這里,它就是表示MainActivity了。
繼續(xù)往下看,下面這個(gè)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 {
- ......
- }