Merge "Destroy the last embedded activity immediately" into tm-dev
diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java
index 791d193..e6be137 100644
--- a/services/core/java/com/android/server/wm/ActivityRecord.java
+++ b/services/core/java/com/android/server/wm/ActivityRecord.java
@@ -3532,7 +3532,7 @@
         final ActivityRecord next = getDisplayArea().topRunningActivity(
                 true /* considerKeyguardState */);
 
-        // If the finishing activity is the last activity of a organized TaskFragment and has an
+        // If the finishing activity is the last activity of an organized TaskFragment and has an
         // adjacent TaskFragment, check if the activity removal should be delayed.
         boolean delayRemoval = false;
         final TaskFragment taskFragment = getTaskFragment();
@@ -3540,7 +3540,8 @@
             final TaskFragment organized = taskFragment.getOrganizedTaskFragment();
             final TaskFragment adjacent =
                     organized != null ? organized.getAdjacentTaskFragment() : null;
-            if (adjacent != null && organized.topRunningActivity() == null) {
+            if (adjacent != null && next.isDescendantOf(adjacent)
+                    && organized.topRunningActivity() == null) {
                 delayRemoval = organized.isDelayLastActivityRemoval();
             }
         }