From 59c156cffb589805f9b63ac3f0e70866a59b1f57 Mon Sep 17 00:00:00 2001 From: Jeff Chang Date: Thu, 12 Nov 2020 14:35:47 +0800 Subject: [PATCH] Avoid creating new instance on top when started for result A new activity instance was created when the task top activity with launch mode singleTop started itself via startActivityForResult. Update the rule for checking if the activity being launched is the same as the one currently at the top. Send the cancel result back if startActivityForResult on the case. Moreover, remove the Single-task condition because it is not working here. Bug: 122967919 Test: atest IntentTests Change-Id: I4c59e8050cb6ddb7430d8141d8454699dc14d2a0 --- .../java/com/android/server/wm/ActivityStarter.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java index 2cc1ea1f774b8..d531666c35e43 100644 --- a/services/core/java/com/android/server/wm/ActivityStarter.java +++ b/services/core/java/com/android/server/wm/ActivityStarter.java @@ -2027,12 +2027,12 @@ class ActivityStarter { */ private int deliverToCurrentTopIfNeeded(Task topStack, NeededUriGrants intentGrants) { final ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(mNotTop); - final boolean dontStart = top != null && mStartActivity.resultTo == null + final boolean dontStart = top != null && top.mActivityComponent.equals(mStartActivity.mActivityComponent) && top.mUserId == mStartActivity.mUserId && top.attachedToProcess() && ((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0 - || isLaunchModeOneOf(LAUNCH_SINGLE_TOP, LAUNCH_SINGLE_TASK)) + || LAUNCH_SINGLE_TOP == mLaunchMode) // This allows home activity to automatically launch on secondary task display area // when it was added, if home was the top activity on default task display area, // instead of sending new intent to the home activity on default display area. @@ -2053,6 +2053,13 @@ class ActivityStarter { return START_RETURN_INTENT_TO_CALLER; } + if (mStartActivity.resultTo != null) { + mStartActivity.resultTo.sendResult(INVALID_UID, mStartActivity.resultWho, + mStartActivity.requestCode, RESULT_CANCELED, + null /* data */, null /* dataGrants */); + mStartActivity.resultTo = null; + } + deliverNewIntent(top, intentGrants); // Don't use mStartActivity.task to show the toast. We're not starting a new activity but