Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ea6e6633 authored by Andrii Kulian's avatar Andrii Kulian
Browse files

Avoid re-adding to existing split

If an activity is re-created after configuration change, make sure
to check if it is not already in a split with an activity below
before creating a new one.

Bug: 190433398
Test: Manual
Change-Id: Ib2b87e3736d6331209be4934303b20717446807c
parent 724ba144
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -147,6 +147,7 @@ public class SplitController implements JetpackTaskFragmentOrganizer.TaskFragmen
     * Checks if the activity start should be routed to a particular container. It can create a new
     * container for the activity and a new split container if necessary.
     */
    // TODO(b/190433398): Break down into smaller functions.
    void onActivityCreated(@NonNull Activity launchedActivity) {
        final ComponentName componentName = launchedActivity.getComponentName();

@@ -200,6 +201,18 @@ public class SplitController implements JetpackTaskFragmentOrganizer.TaskFragmen
            return;
        }

        // Check if the split is already set.
        final TaskFragmentContainer activityBelowContainer = getContainerWithActivity(
                activityBelow.getActivityToken());
        if (currentContainer != null && activityBelowContainer != null) {
            final SplitContainer existingSplit = getActiveSplitForContainers(currentContainer,
                    activityBelowContainer);
            if (existingSplit != null) {
                // There is already an active split with the activity below.
                return;
            }
        }

        final ExtensionSplitPairRule splitPairRule = getSplitRule(
                activityBelow.getComponentName(), componentName, splitRules);
        if (splitPairRule == null) {
@@ -337,7 +350,7 @@ public class SplitController implements JetpackTaskFragmentOrganizer.TaskFragmen
    }

    /**
     * Returns the top active split container that has the provided container.
     * Returns the top active split container that has the provided container, if available.
     */
    @Nullable
    private SplitContainer getActiveSplitForContainer(@NonNull TaskFragmentContainer container) {
@@ -351,6 +364,26 @@ public class SplitController implements JetpackTaskFragmentOrganizer.TaskFragmen
        return null;
    }

    /**
     * Returns the active split that has the provided containers as primary and secondary or as
     * secondary and primary, if available.
     */
    @Nullable
    private SplitContainer getActiveSplitForContainers(
            @NonNull TaskFragmentContainer firstContainer,
            @NonNull TaskFragmentContainer secondContainer) {
        for (int i = mSplitContainers.size() - 1; i >= 0; i--) {
            SplitContainer splitContainer = mSplitContainers.get(i);
            final TaskFragmentContainer primary = splitContainer.getPrimaryContainer();
            final TaskFragmentContainer secondary = splitContainer.getSecondaryContainer();
            if ((firstContainer == secondary && secondContainer == primary)
                    || (firstContainer == primary && secondContainer == secondary)) {
                return splitContainer;
            }
        }
        return null;
    }

    /**
     * Checks if the container requires a placeholder and launches it if necessary.
     */