From eccc88a8b5c628c99d569ec8bdf2d66b9373a747 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Wed, 4 Aug 2021 17:04:51 -0700 Subject: [PATCH] Destroy ActivityRecord when it is removed from the hierarchy When activity is finishing from back, it may be added to the ActivityTaskSupervisor#mFinishingActivities and wait for the resume activity IDLE before destroy. However, TaskFragmentOrganizer will be notified to apply WCT to delete the TaskFragment that has no running activity before that is done. It will cause the activity removed from the hierarchy before the destroy is called. Fix: 195454840 Test: Verify with Sidecar + Demo app Change-Id: I60d9217d061ee53421661bde496b5fb320f01b87 --- .../com/android/server/wm/ActivityRecord.java | 2 +- .../android/server/wm/ActivityRecordTests.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java index bc5ace8a3acff..a45e8e08776c5 100644 --- a/services/core/java/com/android/server/wm/ActivityRecord.java +++ b/services/core/java/com/android/server/wm/ActivityRecord.java @@ -3687,7 +3687,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A @Override void removeImmediately() { - if (!finishing) { + if (!isState(DESTROYING, DESTROYED)) { // If Task#removeImmediately is called directly with alive activities, ensure that the // activities are destroyed and detached from process. destroyImmediately("removeImmediately"); diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java index 41f2246858b44..54406c2b21aad 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java @@ -1615,6 +1615,21 @@ public class ActivityRecordTests extends WindowTestsBase { assertFalse(wpc.hasActivities()); } + @Test + public void testRemoveImmediatelyWithFinishingActivity() throws RemoteException { + final ActivityRecord activity = createActivityWithTask(); + final WindowProcessController wpc = activity.app; + activity.makeFinishingLocked(); + assertTrue(activity.finishing); + + activity.getTask().removeImmediately("test"); + + verify(mAtm.getLifecycleManager()).scheduleTransaction(any(), eq(activity.appToken), + isA(DestroyActivityItem.class)); + assertFalse(wpc.hasActivities()); + assertEquals(DESTROYING, activity.getState()); + } + @Test public void testRemoveFromHistory() { final ActivityRecord activity = createActivityWithTask();