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

問題現象:

由于設備有兩個Display設備,分為主屏和副屏,正常情況下startActivity如果不指定displayId的話,都會默認顯示在主屏上,但是在某些情況下launcher啟動的應用Activity會顯示到副屏上面去。

根據現象來分析,初步判斷問題原因是startActivity的流程出現異常導致的,那么就需要從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函數中有對task和stack的變更與displayId相關的操作,重點看這個地方:

// 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(); //在添加打印后發現出現問題的時候是走的這個函數進行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這個函數干了些什么:

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 */);

? ? ? ? ? ? }

? ? ? ? }

}

根據注釋來看,關鍵就是這個moveStackAllowed了,打印log發現出現問題的時候moveStackAllowed?為true,所以會對mTargetStack?進行重新賦值的操作,在重新賦值操作過程中,由于當前topTask所在的stack是副屏的,所以會進入mService.mStackSupervisor.getNextValidLaunchStackLocked查找非-1的displayId對應的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內部可以看到按照displayId由小到大的順序依次添加到mTmpOrderedDisplayIds這個列表中去,然后再由大到小依次遍歷mTmpOrderedDisplayIds非-1的displayId對應的ActivityStack,由于設備的主屏和副屏的displayId分別是0、1,所以最先查到的就是displayId為1的ActivityStack,這樣activity就會顯示到副屏對應的ActivityStack上,從而導致默認啟動的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對應的displayId不等于startActivity傳入的targetDisplayId,那么就在getNextValidLaunchStackLocked中傳入topStack.mDisplayId排除掉對應的topStack就行了。

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

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

? ? ? ? ? ? ? ? }

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

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

? ? ? ? ? ? }

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容