Prevents recursively moving activity to a new expanded container

When an activity should be expanded, the activity is moved to a
new expanded TaskFragmentContainer and the same container was
splitted with the SplitPinContainer in the same WCT. However,
the activity may be relaunched afterward when updating the
TaskFragment bound (or could also due to insets changes). In
that case, the activity will be resolved to move to a new
expanded TaskFragmentContainer again while Activity#onCreate
is called. So, TaskFragments are created (and removed) repeatedly
for the expanded Activity.

This issue is not reproducible if the activity can handle the
configuration change.

Do nothing if the expanded activity is already in a standalone
container and split with another pinned container.

Bug: 355595391
Test: verified locally with sample app
Flag: EXEMPT bugfix
Change-Id: I8d92788287d72e1a58d1ad383c25585ea7fc4a19
diff --git a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/SplitController.java b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/SplitController.java
index 8e1fde0..5d5d751f 100644
--- a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/SplitController.java
+++ b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/SplitController.java
@@ -1333,13 +1333,24 @@
         if (shouldContainerBeExpanded(container)) {
             // Make sure that the existing container is expanded.
             mPresenter.expandTaskFragment(wct, container);
-        } else {
-            // Put activity into a new expanded container.
-            final TaskFragmentContainer newContainer =
-                    new TaskFragmentContainer.Builder(this, getTaskId(activity), activity)
-                            .setPendingAppearedActivity(activity).build();
-            mPresenter.expandActivity(wct, newContainer.getTaskFragmentToken(), activity);
+            return;
         }
+
+        final SplitContainer splitContainer = getActiveSplitForContainer(container);
+        if (splitContainer instanceof SplitPinContainer
+                && !container.isPinned() && container.getRunningActivityCount() == 1) {
+            // This is already the expected state when the pinned container is shown with an
+            // expanded activity in a standalone container on the side. Moving the activity into
+            // another new expanded container again is not necessary and could result in
+            // recursively creating new TaskFragmentContainers if the activity somehow relaunched.
+            return;
+        }
+
+        // Put activity into a new expanded container.
+        final TaskFragmentContainer newContainer =
+                new TaskFragmentContainer.Builder(this, getTaskId(activity), activity)
+                        .setPendingAppearedActivity(activity).build();
+        mPresenter.expandActivity(wct, newContainer.getTaskFragmentToken(), activity);
     }
 
     /**