Android8.0雙屏顯示異常捣作,主屏啟動的Activity會顯示到副屏上的問題

問題現(xiàn)象:

由于設(shè)備有兩個Display設(shè)備测柠,分為主屏和副屏炼鞠,正常情況下startActivity如果不指定displayId的話缘滥,都會默認(rèn)顯示在主屏上,但是在某些情況下launcher啟動的應(yīng)用Activity會顯示到副屏上面去谒主。

根據(jù)現(xiàn)象來分析朝扼,初步判斷問題原因是startActivity的流程出現(xiàn)異常導(dǎo)致的,那么就需要從startActivity流程開始查找原因霎肯。

先看framework\base\services\core\java\com\android\server\am\ActivityStarter.java中的:

```

int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,

? ? ? ? ? ?...) {

? ? ? ? ...

? ? ? ? mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,

? ? ? ? ? ? ? ? aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,

? ? ? ? ? ? ? ? callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,

? ? ? ? ? ? ? ? options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,

? ? ? ? ? ? ? ? inTask);

? ? ? ? ...

? ? }

```

一步一步往下查之后看到startActivityUnchecked函數(shù)中有對task和stack的變更與displayId相關(guān)的操作擎颖,重點(diǎn)看這個地方:

// Note: This method should only be called from {@link startActivity}.

? ? private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,

? ? ? ? ? ? IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

? ? ? ? ? ? int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,

? ? ? ? ? ? ActivityRecord[] outActivity) {

...

// Should this be considered a new task?

? ? ? ? int result = START_SUCCESS;

? ? ? ? if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask

? ? ? ? ? ? ? ? && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {

? ? ? ? ? ? newTask = true;

? ? ? ? ? ? String packageName= mService.mContext.getPackageName();

? ? ? ? ? ? if (mPerf != null) {

? ? ? ? ? ? ? ? mStartActivity.perfActivityBoostHandler =

? ? ? ? ? ? ? ? ? ? mPerf.perfHint(BoostFramework.VENDOR_HINT_FIRST_LAUNCH_BOOST, packageName, -1, BoostFramework.Launch.BOOST_V1);

? ? ? ? ? ? }

? ? ? ? ? ? result = setTaskFromReuseOrCreateNewTask(?

? ? ? ? ? ? ? ? ? ? taskToAffiliate, preferredLaunchStackId, topStack);

? ? ? ? } else if (mSourceRecord != null) {

? ? ? ? ? ? result = setTaskFromSourceRecord(); //在添加打印后發(fā)現(xiàn)出現(xiàn)問題的時候是走的這個函數(shù)進(jìn)行task與stack操作的

? ? ? ? } else if (mInTask != null) {

? ? ? ? ? ? result = setTaskFromInTask();

? ? ? ? } else {

? ? ? ? ? ? // This not being started from an existing activity, and not part of a new task...

? ? ? ? ? ? // just put it in the top task, though these days this case should never happen.

? ? ? ? ? ? setTaskToCurrentTopOrCreateNewTask();

? ? ? ? }

...

}


那么就看setTaskFromSourceRecord這個函數(shù)干了些什么:

private int setTaskFromSourceRecord() {

...

? ? ? ? // We only want to allow changing stack in two cases:

? ? ? ? // 1. If the target task is not the top one. Otherwise we would move the launching task to

? ? ? ? //? ? the other side, rather than show two side by side.

? ? ? ? // 2. If activity is not allowed on target display.

? ? ? ? final int targetDisplayId = mTargetStack != null ? mTargetStack.mDisplayId

? ? ? ? ? ? ? ? : sourceStack.mDisplayId;

????????final boolean moveStackAllowed = sourceStack.topTask() != sourceTask

? ? ? ? ? ? ? ? || !mStartActivity.canBeLaunchedOnDisplay(targetDisplayId);

????????if (moveStackAllowed) {

? ? ? ? ? ? mTargetStack = getLaunchStack(mStartActivity, mLaunchFlags, mStartActivity.getTask(),

? ? ? ? ? ? ? ? ? ? mOptions);

? ? ? ? ? ? // If target stack is not found now - we can't just rely on the source stack, as it may

? ? ? ? ? ? // be not suitable. Let's check other displays.

? ? ? ? ? ? if (mTargetStack == null && targetDisplayId != sourceStack.mDisplayId) {

? ? ? ? ? ? ? ? // Can't use target display, lets find a stack on the source display.

? ? ? ? ? ? ? ? mTargetStack = mService.mStackSupervisor.getValidLaunchStackOnDisplay(

? ? ? ? ? ? ? ? ? ? ? ? sourceStack.mDisplayId, mStartActivity);

? ? ? ? ? ? }

? ? ? ? ? ? if (mTargetStack == null) {

? ? ? ? ? ? ? ? // There are no suitable stacks on the target and source display(s). Look on all

? ? ? ? ? ? ? ? // displays.

? ? ? ? ? ? ? ? mTargetStack = mService.mStackSupervisor.getNextValidLaunchStackLocked(

? ? ? ? ? ? ? ? ? ? ? ? mStartActivity, -1/* currentFocus */);

? ? ? ? ? ? }

? ? ? ? }

}

根據(jù)注釋來看,關(guān)鍵就是這個moveStackAllowed了观游,打印log發(fā)現(xiàn)出現(xiàn)問題的時候moveStackAllowed?為true搂捧,所以會對mTargetStack?進(jìn)行重新賦值的操作,在重新賦值操作過程中懂缕,由于當(dāng)前topTask所在的stack是副屏的允跑,所以會進(jìn)入mService.mStackSupervisor.getNextValidLaunchStackLocked查找非-1的displayId對應(yīng)的ActivityStack來賦值。

/**

? ? * Get next valid stack for launching provided activity in the system. This will search across

? ? * displays and stacks in last-focused order for a focusable and visible stack, except those

? ? * that are on a currently focused display.

? ? *

? ? * @param r The activity that is being launched.

? ? * @param currentFocus The display that previously had focus and thus needs to be ignored when

? ? *? ? ? ? ? ? ? ? ? ? searching for the next candidate.

? ? * @return Next valid {@link ActivityStack}, null if not found.

? ? */

? ? ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {

? ? ? ? mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);

? ? ? ? for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {

? ? ? ? ? ? final int displayId = mTmpOrderedDisplayIds.get(i);

? ? ? ? ? ? if (displayId == currentFocus) {

? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? }

? ? ? ? ? ? final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);

? ? ? ? ? ? if (stack != null) {

? ? ? ? ? ? ? ? return stack;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return null;

? ? }

從getNextValidLaunchStackLocked內(nèi)部可以看到按照displayId由小到大的順序依次添加到mTmpOrderedDisplayIds這個列表中去搪柑,然后再由大到小依次遍歷mTmpOrderedDisplayIds非-1的displayId對應(yīng)的ActivityStack聋丝,由于設(shè)備的主屏和副屏的displayId分別是0、1工碾,所以最先查到的就是displayId為1的ActivityStack潮针,這樣activity就會顯示到副屏對應(yīng)的ActivityStack上,從而導(dǎo)致默認(rèn)啟動的activity在副屏上顯示的問題倚喂。

知道原因就好辦了每篷,直接在setTaskFromSourceRecord中添加判斷:

if (mTargetStack == null) {

? ? ? ? ? ? ? ? // There are no suitable stacks on the target and source display(s). Look on all

? ? ? ? ? ? ? ? // displays.

? ? ? ? ? ? ? ? ActivityStack topStack = mSupervisor.mFocusedStack;

? ? ? ? ? ? ? ? int currentFocusDisplayId = -1;

? ? ? ? ? ? ? ? //如果topStack對應(yīng)的displayId不等于startActivity傳入的targetDisplayId,那么就在getNextValidLaunchStackLocked中傳入topStack.mDisplayId排除掉對應(yīng)的topStack就行了端圈。

? ? ? ? ? ? ? ? if (topStack != null && topStack.mDisplayId != targetDisplayId) {

? ? ? ? ? ? ? ? ? ? currentFocusDisplayId = topStack.mDisplayId;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? mTargetStack = mService.mStackSupervisor.getNextValidLaunchStackLocked(

? ? ? ? ? ? ? ? ? ? ? ? mStartActivity, currentFocusDisplayId /* currentFocus */);

? ? ? ? ? ? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末焦读,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子舱权,更是在濱河造成了極大的恐慌矗晃,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宴倍,死亡現(xiàn)場離奇詭異张症,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鸵贬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門俗他,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人阔逼,你說我怎么就攤上這事兆衅。” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵羡亩,是天一觀的道長摩疑。 經(jīng)常有香客問我,道長畏铆,這世上最難降的妖魔是什么雷袋? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮辞居,結(jié)果婚禮上片排,老公的妹妹穿的比我還像新娘。我一直安慰自己速侈,他們只是感情好率寡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著倚搬,像睡著了一般冶共。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上每界,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天捅僵,我揣著相機(jī)與錄音,去河邊找鬼眨层。 笑死庙楚,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的趴樱。 我是一名探鬼主播馒闷,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼叁征!你這毒婦竟也來了纳账?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤捺疼,失蹤者是張志新(化名)和其女友劉穎疏虫,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體啤呼,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卧秘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了官扣。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片翅敌。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖醇锚,靈堂內(nèi)的尸體忽然破棺而出哼御,到底是詐尸還是另有隱情坯临,我是刑警寧澤焊唬,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布恋昼,位于F島的核電站,受9級特大地震影響赶促,放射性物質(zhì)發(fā)生泄漏液肌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一鸥滨、第九天 我趴在偏房一處隱蔽的房頂上張望嗦哆。 院中可真熱鬧,春花似錦婿滓、人聲如沸老速。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽橘券。三九已至,卻和暖如春卿吐,著一層夾襖步出監(jiān)牢的瞬間旁舰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工嗡官, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留箭窜,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓衍腥,卻偏偏與公主長得像磺樱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子婆咸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容