# SplitController

Artifact: [androidx.window:window](https://developer.android.com/jetpack/androidx/releases/window) [View Source](https://cs.android.com/search?q=file:androidx/window/embedding/SplitController.kt+class:androidx.window.embedding.SplitController) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/window#1.0.0)

*** ** * ** ***


```
public final class SplitController
```

<br />

*** ** * ** ***

The controller class that gets information about the currently active activity splits and provides interaction points to customize the splits and form new splits.

A split is a pair of containers that host activities in the same or different processes, combined under the same parent window of the hosting task.

A pair of activities can be put into a split by providing a static or runtime split rule and then launching the activities in the same task using `https://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)`.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/window/embedding/SplitController.SplitSupportStatus` A class to determine if activity splits with Activity Embedding are currently available. |

| ### Public methods |
|---|---|
| `final void` | `@https://developer.android.com/reference/androidx/window/RequiresWindowSdkExtension(version = 2) https://developer.android.com/reference/androidx/window/embedding/SplitController#clearSplitAttributesCalculator()()` Clears the callback previously set by `https://developer.android.com/reference/androidx/window/embedding/SplitController#setSplitAttributesCalculator(kotlin.Function1)`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitController` | `https://developer.android.com/reference/androidx/window/embedding/SplitController#getInstance(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` Obtains an instance of `https://developer.android.com/reference/androidx/window/embedding/SplitController`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitController.SplitSupportStatus` | `https://developer.android.com/reference/androidx/window/embedding/SplitController#getSplitSupportStatus()()` Indicates whether split functionality is supported on the device. |
| `final boolean` | `@https://developer.android.com/reference/androidx/window/RequiresWindowSdkExtension(version = 5) https://developer.android.com/reference/androidx/window/embedding/SplitController#pinTopActivityStack(kotlin.Int,androidx.window.embedding.SplitPinRule)(int taskId, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitPinRule splitPinRule)` Pins the top-most `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` to keep the stack of the Activities to be always positioned on top. |
| `final void` | `@https://developer.android.com/reference/androidx/window/RequiresWindowSdkExtension(version = 2) https://developer.android.com/reference/androidx/window/embedding/SplitController#setSplitAttributesCalculator(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitAttributesCalculatorParams, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitAttributes> calculator )` Sets or replaces the previously registered `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` calculator. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitInfo>>` | `https://developer.android.com/reference/androidx/window/embedding/SplitController#splitInfoList(android.app.Activity)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity)` A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of `https://developer.android.com/reference/androidx/window/embedding/SplitInfo` list that contains the current split states that this `https://developer.android.com/reference/androidx/window/embedding/SplitController#splitInfoList(android.app.Activity)` is part of. |
| `final void` | `@https://developer.android.com/reference/androidx/window/RequiresWindowSdkExtension(version = 5) https://developer.android.com/reference/androidx/window/embedding/SplitController#unpinTopActivityStack(kotlin.Int)(int taskId)` Unpins the pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`. |
| `final void` | `@https://developer.android.com/reference/androidx/window/RequiresWindowSdkExtension(version = 3) https://developer.android.com/reference/androidx/window/embedding/SplitController#updateSplitAttributes(androidx.window.embedding.SplitInfo,androidx.window.embedding.SplitAttributes)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitInfo splitInfo, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitAttributes splitAttributes )` Updates the `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` of a split pair. |

## Public methods

### clearSplitAttributesCalculator

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/window#1.1.0)

```
@RequiresWindowSdkExtension(version = 2)
public final void clearSplitAttributesCalculator()
```

Clears the callback previously set by `https://developer.android.com/reference/androidx/window/embedding/SplitController#setSplitAttributesCalculator(kotlin.Function1)`. The caller **must** make sure if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is greater than or equal to 2.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is less than 2. |

### getInstance

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/window#1.1.0)

```
public static final @NonNull SplitController getInstance(@NonNull Context context)
```

Obtains an instance of `https://developer.android.com/reference/androidx/window/embedding/SplitController`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | the `https://developer.android.com/reference/android/content/Context.html` to initialize the controller with |

### getSplitSupportStatus

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/window#1.1.0)

```
public final @NonNull SplitController.SplitSupportStatus getSplitSupportStatus()
```

Indicates whether split functionality is supported on the device. Note that devices might not enable splits in all states or conditions. For example, a foldable device with multiple screens can choose to collapse splits when apps run on the device's small display, but enable splits when apps run on the device's large display. In cases like this, `https://developer.android.com/reference/androidx/window/embedding/SplitController#splitSupportStatus()` always returns `https://developer.android.com/reference/androidx/window/embedding/SplitController.SplitSupportStatus#SPLIT_AVAILABLE()`, and if the split is collapsed, activities are launched on top, following the non-activity embedding model.

Also the `https://developer.android.com/reference/androidx/window/WindowProperties#PROPERTY_ACTIVITY_EMBEDDING_SPLITS_ENABLED()` must be enabled in AndroidManifest within in order to get the correct state or `https://developer.android.com/reference/androidx/window/embedding/SplitController.SplitSupportStatus#SPLIT_ERROR_PROPERTY_NOT_DECLARED()` will be returned in some cases.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/window/embedding/SplitController.SplitSupportStatus` |   |

### pinTopActivityStack

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/window#1.4.0)

```
@RequiresWindowSdkExtension(version = 5)
public final boolean pinTopActivityStack(int taskId, @NonNull SplitPinRule splitPinRule)
```

Pins the top-most `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` to keep the stack of the Activities to be always positioned on top. The rest of the activities in the Task will be split with the pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`. The pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` would also have isolated activity navigation in which only the activities that are started from the pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` can be added on top of the `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`.

The pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` is unpinned whenever the pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` is expanded. Use `https://developer.android.com/reference/androidx/window/embedding/SplitPinRule.Builder#setSticky(kotlin.Boolean)` if the same `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` should be pinned again whenever the `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` is on top and split with another `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` again.

The caller **must** make sure if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is greater than or equal to 5.

| Parameters |
|---|---|
| `int taskId` | The id of the Task that top `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` should be pinned. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitPinRule splitPinRule` | The SplitRule that specifies how the top `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` should be split with others. |

| Returns |
|---|---|
| `boolean` | Returns `true` if the top `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` is successfully pinned. Otherwise, `false`. Few examples are: 1. There's no `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`. 2. There is already an existing pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`. 3. There's no other `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` to split with the top `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is less than 5. |

### setSplitAttributesCalculator

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/window#1.1.0)

```
@RequiresWindowSdkExtension(version = 2)
public final void setSplitAttributesCalculator(
    @NonNull Function1<@NonNull SplitAttributesCalculatorParams, @NonNull SplitAttributes> calculator
)
```

Sets or replaces the previously registered `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` calculator.

**Note** that it's callers' responsibility to check if this API is supported by checking `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` before using the this API. It is suggested to always set meaningful `https://developer.android.com/reference/androidx/window/embedding/SplitRule#defaultSplitAttributes()` in case this API is not supported on some devices.

Also, replacing the calculator will only update existing split pairs after a change in the window or device state, such as orientation changes or folding state changes.

The `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` calculator is a function to compute the current `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` for the given `https://developer.android.com/reference/androidx/window/embedding/SplitRule` with the current device and window state. Then The calculator will be invoked if either:

- An activity is started and matches a registered `https://developer.android.com/reference/androidx/window/embedding/SplitRule`.

- A parent configuration is updated and there's an existing split pair.

By default, `https://developer.android.com/reference/androidx/window/embedding/SplitRule#defaultSplitAttributes()` are applied if the parent container's `https://developer.android.com/reference/androidx/window/layout/WindowMetrics` satisfies the `https://developer.android.com/reference/androidx/window/embedding/SplitRule`'s dimensions requirements, which are `https://developer.android.com/reference/androidx/window/embedding/SplitRule#minWidthDp()`, `https://developer.android.com/reference/androidx/window/embedding/SplitRule#minHeightDp()` and `https://developer.android.com/reference/androidx/window/embedding/SplitRule#minSmallestWidthDp()`. The `https://developer.android.com/reference/androidx/window/embedding/SplitRule#defaultSplitAttributes()` can be set by

- `https://developer.android.com/reference/androidx/window/embedding/SplitRule` Builder APIs, which are `https://developer.android.com/reference/androidx/window/embedding/SplitPairRule.Builder#setDefaultSplitAttributes(androidx.window.embedding.SplitAttributes)` and `https://developer.android.com/reference/androidx/window/embedding/SplitPlaceholderRule.Builder#setDefaultSplitAttributes(androidx.window.embedding.SplitAttributes)`.

- Specifying with `splitRatio` and `splitLayoutDirection` attributes in `<SplitPairRule>` or `<SplitPlaceHolderRule>` tags in XML files.

Developers may want to apply different `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` for different device or window states. For example, on foldable devices, developers may want to split the screen vertically if the device is in landscape, fill the screen if the device is in portrait and split the screen horizontally if the device is in [tabletop posture](https://developer.android.com/guide/topics/ui/foldables#postures). In this case, the `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` can be customized by the `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` calculator, which takes effects after calling this API. Developers can also clear the calculator by `https://developer.android.com/reference/androidx/window/embedding/SplitController#clearSplitAttributesCalculator()`. Then, developers could implement the `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` calculator as the sample linked below shows.

```kotlin
import androidx.window.embedding.EmbeddingAnimationBackground
import androidx.window.embedding.EmbeddingAnimationParams
import androidx.window.embedding.SplitAttributes
import androidx.window.embedding.SplitAttributes.SplitType.Companion.SPLIT_TYPE_EQUAL
import androidx.window.embedding.SplitAttributes.SplitType.Companion.SPLIT_TYPE_EXPAND
import androidx.window.embedding.SplitAttributes.SplitType.Companion.SPLIT_TYPE_HINGE
import androidx.window.embedding.SplitController
import androidx.window.layout.FoldingFeature

SplitController.getInstance(context).setSplitAttributesCalculator { params ->
    val tag = params.splitRuleTag
    val parentWindowMetrics = params.parentWindowMetrics
    val parentConfig = params.parentConfiguration
    val foldingFeatures =
        params.parentWindowLayoutInfo.displayFeatures.filterIsInstance<FoldingFeature>()
    val foldingState = if (foldingFeatures.size == 1) foldingFeatures[0] else null
    // Tag can be used to filter the SplitRule to apply the SplitAttributes
    if (TAG_SPLIT_RULE_MAIN != tag && params.areDefaultConstraintsSatisfied) {
        return@setSplitAttributesCalculator params.defaultSplitAttributes
    }

    // This sample will make the app show a layout to
    // - split the task bounds vertically if the device is in landscape
    // - fill the task bounds if the device is in portrait and its folding state does not
    //   split the screen
    // - split the task bounds horizontally in tabletop mode
    val bounds = parentWindowMetrics.bounds
    if (foldingState?.isSeparating == true) {
        // Split the parent container that followed by the hinge if the hinge separates the
        // parent window.
        return@setSplitAttributesCalculator SplitAttributes.Builder()
            .setSplitType(SPLIT_TYPE_HINGE)
            .setLayoutDirection(
                if (foldingState.orientation == FoldingFeature.Orientation.HORIZONTAL) {
                    SplitAttributes.LayoutDirection.TOP_TO_BOTTOM
                } else {
                    SplitAttributes.LayoutDirection.LOCALE
                }
            )
            // Optionally set the animation background and change transition animation to use
            // when switching between vertical and horizontal
            .setAnimationParams(
                EmbeddingAnimationParams.Builder()
                    .setAnimationBackground(
                        EmbeddingAnimationBackground.createColorBackground(Color.GRAY)
                    )
                    .setChangeAnimation(EmbeddingAnimationParams.AnimationSpec.JUMP_CUT)
                    .build()
            )
            .build()
    }
    return@setSplitAttributesCalculator if (
        parentConfig.screenWidthDp >= 600 && bounds.width() >= bounds.height()
    ) {
        // Split the parent container equally and vertically if the device is in landscape.
        SplitAttributes.Builder()
            .setSplitType(SPLIT_TYPE_EQUAL)
            .setLayoutDirection(SplitAttributes.LayoutDirection.LOCALE)
            // Optionally set the animation background and change transition animation to use
            // when switching between vertical and horizontal
            .setAnimationParams(
                EmbeddingAnimationParams.Builder()
                    .setAnimationBackground(
                        EmbeddingAnimationBackground.createColorBackground(Color.GRAY)
                    )
                    .setChangeAnimation(EmbeddingAnimationParams.AnimationSpec.JUMP_CUT)
                    .build()
            )
            .build()
    } else {
        // Expand containers if the device is in portrait or the width is less than 600 dp.
        SplitAttributes.Builder().setSplitType(SPLIT_TYPE_EXPAND).build()
    }
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitAttributesCalculatorParams, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitAttributes> calculator` | the function to calculate `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` based on the `https://developer.android.com/reference/androidx/window/embedding/SplitAttributesCalculatorParams`. It will replace the previously set if it exists. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is less than 2. |

### splitInfoList

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/window#1.1.0)

```
public final @NonNull Flow<@NonNull List<@NonNull SplitInfo>> splitInfoList(@NonNull Activity activity)
```

A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of `https://developer.android.com/reference/androidx/window/embedding/SplitInfo` list that contains the current split states that this `https://developer.android.com/reference/androidx/window/embedding/SplitController#splitInfoList(android.app.Activity)` is part of.

An activity can be in zero, one or more `https://developer.android.com/reference/androidx/window/embedding/SplitInfo`. More than one active split is possible if an activity created multiple containers to side, stacked on top of each other. Or it can be in two different splits at the same time - in a secondary container for one (it was launched to the side) and in the primary for another (it launched another activity to the side). The reported splits in the list are ordered from bottom to top by their z-order, more recent splits appearing later. Guaranteed to be called at least once to report the most recent state.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity` | The `https://developer.android.com/reference/android/app/Activity.html` that is interested in getting the split states |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitInfo>>` | a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` of `https://developer.android.com/reference/androidx/window/embedding/SplitInfo` list that includes this `https://developer.android.com/reference/androidx/window/embedding/SplitController#splitInfoList(android.app.Activity)` |

### unpinTopActivityStack

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/window#1.4.0)

```
@RequiresWindowSdkExtension(version = 5)
public final void unpinTopActivityStack(int taskId)
```

Unpins the pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`. The `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` will still be the top-most `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` right after unpinned, and the `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` could be expanded or continue to be split with the next top `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` if the current state matches any of the existing `https://developer.android.com/reference/androidx/window/embedding/SplitPairRule`. It is a no-op call if the task does not have a pinned `https://developer.android.com/reference/androidx/window/embedding/ActivityStack`.

The caller **must** make sure if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is greater than or equal to 5.

| Parameters |
|---|---|
| `int taskId` | The id of the Task that top `https://developer.android.com/reference/androidx/window/embedding/ActivityStack` should be unpinned. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is less than 5. |

### updateSplitAttributes

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/window#1.2.0)

```
@RequiresWindowSdkExtension(version = 3)
public final void updateSplitAttributes(
    @NonNull SplitInfo splitInfo,
    @NonNull SplitAttributes splitAttributes
)
```

Updates the `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` of a split pair. This is an alternative to using a split attributes calculator callback set in `https://developer.android.com/reference/androidx/window/embedding/SplitController#setSplitAttributesCalculator(kotlin.Function1)`, useful when apps only need to update the splits in a few cases proactively but rely on the default split attributes most of the time otherwise.

The provided split attributes will be used instead of the associated `https://developer.android.com/reference/androidx/window/embedding/SplitRule#defaultSplitAttributes()`.

**Note** that the split attributes may be updated if split attributes calculator callback is registered and invoked. If `https://developer.android.com/reference/androidx/window/embedding/SplitController#setSplitAttributesCalculator(kotlin.Function1)` is used, the callback will still be applied to each `https://developer.android.com/reference/androidx/window/embedding/SplitInfo` when there's either:

- A new Activity being launched.

- A window or device state updates (e,g. due to screen rotation or folding state update).

In most cases it is suggested to use `https://developer.android.com/reference/androidx/window/embedding/ActivityEmbeddingController#invalidateVisibleActivityStacks()` if a calculator has been set through `https://developer.android.com/reference/androidx/window/embedding/SplitController#setSplitAttributesCalculator(kotlin.Function1)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitInfo splitInfo` | the split pair to update |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/window/embedding/SplitAttributes splitAttributes` | the `https://developer.android.com/reference/androidx/window/embedding/SplitAttributes` to be applied |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if `https://developer.android.com/reference/androidx/window/WindowSdkExtensions#extensionVersion()` is less than 3. |