# ScreenManager

Artifact: [androidx.car.app:app](https://developer.android.com/jetpack/androidx/releases/car-app) [View Source](https://cs.android.com/search?q=file:androidx/car/app/ScreenManager.java+class:androidx.car.app.ScreenManager) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

*** ** * ** ***


```
@MainThread
class ScreenManager : Manager
```

<br />

Known direct subclasses [TestScreenManager](https://developer.android.com/reference/kotlin/androidx/car/app/testing/TestScreenManager)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/car/app/testing/TestScreenManager` | The `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager` that is used for testing. |

*** ** * ** ***

Manages the stack of `https://developer.android.com/reference/kotlin/androidx/car/app/Screen`s and their respective `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`s.

## Summary

| ### Public functions |
|---|---|
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-collection/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<https://developer.android.com/reference/kotlin/androidx/car/app/Screen!>` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#getScreenStack()()` Returns the copy of the current screen stack as a type `https://developer.android.com/reference/java/util/Collection.html` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#getStackSize()()` Returns the current stack size. |
| `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#getTop()()` Returns the `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` that is at the top of the stack. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#pop()()` Pops the top `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` from the stack. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#popTo(java.lang.String)(marker: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Removes screens from the top of the stack until a `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` which has the given ` marker` is found, or the root has been reached. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#popToRoot()()` Removes all screens from the stack until the root has been reached. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#push(androidx.car.app.Screen)(screen: https://developer.android.com/reference/kotlin/androidx/car/app/Screen)` Pushes the `screen` to the stack. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#pushForResult(androidx.car.app.Screen,androidx.car.app.OnScreenResultListener)( screen: https://developer.android.com/reference/kotlin/androidx/car/app/Screen, onScreenResultListener: https://developer.android.com/reference/kotlin/androidx/car/app/OnScreenResultListener )` Pushes a `https://developer.android.com/reference/kotlin/androidx/car/app/Screen`, for which you would like a result from, onto the stack. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#remove(androidx.car.app.Screen)(screen: https://developer.android.com/reference/kotlin/androidx/car/app/Screen)` Removes the `screen` from the stack. |

## Public functions

### getScreenStack

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

```
fun getScreenStack(): (Mutable)Collection<Screen!>
```

Returns the copy of the current screen stack as a type `https://developer.android.com/reference/java/util/Collection.html`

### getStackSize

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

```
fun getStackSize(): Int
```

Returns the current stack size.

### getTop

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

```
fun getTop(): Screen
```

Returns the `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` that is at the top of the stack.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if the method is called before a `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` has been pushed to the stack via `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#push(androidx.car.app.Screen)`, or `https://developer.android.com/reference/kotlin/androidx/car/app/ScreenManager#pushForResult(androidx.car.app.Screen,androidx.car.app.OnScreenResultListener)`, or returning a `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` from `https://developer.android.com/reference/kotlin/androidx/car/app/Session#onCreateScreen(android.content.Intent)` |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the current thread is not the main thread |

### pop

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

```
fun pop(): Unit
```

Pops the top `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` from the stack.

If the top `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` is the only `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` in the stack, it will not be removed.

If the app's lifecycle is already in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, this operation is a no-op.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the current thread is not the main thread |

### popTo

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

```
fun popTo(marker: String): Unit
```

Removes screens from the top of the stack until a `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` which has the given `
marker` is found, or the root has been reached.

The root `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` will not be popped.

If the app's lifecycle is already in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, this operation is a no-op.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `marker` is `null` |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the current thread is not the main thread |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/car/app/Screen#setMarker(java.lang.String)` |   |

### popToRoot

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

```
fun popToRoot(): Unit
```

Removes all screens from the stack until the root has been reached.

If the app's lifecycle is already in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, this operation is a no-op.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the current thread is not the main thread |

### push

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

```
fun push(screen: Screen): Unit
```

Pushes the `screen` to the stack.

If the `screen` pushed is already in the stack it will be moved to the top of the stack.

If the app's lifecycle is already in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, this operation is a no-op.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `screen` is `null` |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the current thread is not the main thread |

### pushForResult

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

```
fun pushForResult(
    screen: Screen,
    onScreenResultListener: OnScreenResultListener
): Unit
```

Pushes a `https://developer.android.com/reference/kotlin/androidx/car/app/Screen`, for which you would like a result from, onto the stack.

When the given `screen` finishes, the `onScreenResultCallback` will receive a callback to `https://developer.android.com/reference/kotlin/androidx/car/app/OnScreenResultListener#onScreenResult(java.lang.Object)` with the result that the pushed `screen` set via `https://developer.android.com/reference/kotlin/androidx/car/app/Screen#setResult(java.lang.Object)`.

If the app's lifecycle is already in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, this operation is a no-op.

| Parameters |
|---|---|
| `screen: https://developer.android.com/reference/kotlin/androidx/car/app/Screen` | the `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` to push on top of the stack |
| `onScreenResultListener: https://developer.android.com/reference/kotlin/androidx/car/app/OnScreenResultListener` | the listener that will be executed with the result pushed by the `screen` through `https://developer.android.com/reference/kotlin/androidx/car/app/Screen#setResult(java.lang.Object)`. This callback will be executed on the main thread |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if either the `screen` or the ` onScreenResultCallback` are `null` |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the current thread is not the main thread |

### remove

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

```
fun remove(screen: Screen): Unit
```

Removes the `screen` from the stack.

If the `screen` is the only `https://developer.android.com/reference/kotlin/androidx/car/app/Screen` in the stack, it will not be removed.

If the app's lifecycle is already in the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, this operation is a no-op.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `screen` is `null` |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the current thread is not the main thread |