From f714906c48119413fbf864b0bc00589e9088e523 Mon Sep 17 00:00:00 2001 From: Jeff Chang Date: Thu, 14 Jan 2021 16:38:02 +0800 Subject: [PATCH] Avoid creating new instance when started for result with singleInstance/singleTask MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The activity with launchMode "singleTask" and "singleInstance" can exist in multiple instances. It contradicts with developers documentation and breaks the purpose of these modes. The conditions for searching the reusable tasks always ignore the start-for-result case result in singleInstance/singleTask instances being created. List the problems : 1. singleInstance activity stays in the caller’s task if it was started via startActivityForResult(). The activity should always get launched into its own task. It should always be the single and only member of its task. 2. Multiple singleInstance/singleTask activities are created and stay in the different tasks. The CL updates the condition for searching the reusable task which avoids the new instance being created. Bug: 122967919 Bug: 148047668 Test: atest IntentTests Change-Id: I721cfcaac0759327ae81f430d52d81f1c8145262 --- .../core/java/com/android/server/wm/ActivityStarter.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java index c6ed16ce1f4dd..8ed15197bb867 100644 --- a/services/core/java/com/android/server/wm/ActivityStarter.java +++ b/services/core/java/com/android/server/wm/ActivityStarter.java @@ -1814,8 +1814,7 @@ class ActivityStarter { } private Task computeTargetTask() { - if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask - && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) { + if (mInTask == null && !mAddingToTask && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) { // A new task should be created instead of using existing one. return null; } else if (mSourceRecord != null) { @@ -2512,7 +2511,9 @@ class ActivityStarter { // If bring to front is requested, and no result is requested and we have not been given // an explicit task to launch in to, and we can find a task that was started with this // same component, then instead of launching bring that one to the front. - putIntoExistingTask &= mInTask == null && mStartActivity.resultTo == null; + putIntoExistingTask &= !isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK) + ? (mInTask == null && mStartActivity.resultTo == null) + : (mInTask == null); ActivityRecord intentActivity = null; if (putIntoExistingTask) { if (LAUNCH_SINGLE_INSTANCE == mLaunchMode) {