# 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
public class ScreenManager implements Manager
```

<br />

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

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

*** ** * ** ***

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

## Summary

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<https://developer.android.com/reference/androidx/car/app/Screen>` | `https://developer.android.com/reference/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` |
| `int` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#getStackSize()()` Returns the current stack size. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/Screen` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#getTop()()` Returns the `https://developer.android.com/reference/androidx/car/app/Screen` that is at the top of the stack. |
| `void` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#pop()()` Pops the top `https://developer.android.com/reference/androidx/car/app/Screen` from the stack. |
| `void` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#popTo(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html marker)` Removes screens from the top of the stack until a `https://developer.android.com/reference/androidx/car/app/Screen` which has the given ` marker` is found, or the root has been reached. |
| `void` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#popToRoot()()` Removes all screens from the stack until the root has been reached. |
| `void` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#push(androidx.car.app.Screen)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/Screen screen)` Pushes the `screen` to the stack. |
| `void` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#pushForResult(androidx.car.app.Screen,androidx.car.app.OnScreenResultListener)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/Screen screen, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/OnScreenResultListener onScreenResultListener )` Pushes a `https://developer.android.com/reference/androidx/car/app/Screen`, for which you would like a result from, onto the stack. |
| `void` | `https://developer.android.com/reference/androidx/car/app/ScreenManager#remove(androidx.car.app.Screen)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/Screen screen)` Removes the `screen` from the stack. |

## Public methods

### getScreenStack

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

```
public @NonNull Collection<Screen> getScreenStack()
```

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)

```
public int getStackSize()
```

Returns the current stack size.

### getTop

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

```
public @NonNull Screen getTop()
```

Returns the `https://developer.android.com/reference/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/androidx/car/app/Screen` has been pushed to the stack via `https://developer.android.com/reference/androidx/car/app/ScreenManager#push(androidx.car.app.Screen)`, or `https://developer.android.com/reference/androidx/car/app/ScreenManager#pushForResult(androidx.car.app.Screen,androidx.car.app.OnScreenResultListener)`, or returning a `https://developer.android.com/reference/androidx/car/app/Screen` from `https://developer.android.com/reference/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)

```
public void pop()
```

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

If the top `https://developer.android.com/reference/androidx/car/app/Screen` is the only `https://developer.android.com/reference/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/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)

```
public void popTo(@NonNull String marker)
```

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

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

If the app's lifecycle is already in the `https://developer.android.com/reference/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/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)

```
public void popToRoot()
```

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/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)

```
public void push(@NonNull Screen screen)
```

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/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)

```
public void pushForResult(
    @NonNull Screen screen,
    @NonNull OnScreenResultListener onScreenResultListener
)
```

Pushes a `https://developer.android.com/reference/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/androidx/car/app/OnScreenResultListener#onScreenResult(java.lang.Object)` with the result that the pushed `screen` set via `https://developer.android.com/reference/androidx/car/app/Screen#setResult(java.lang.Object)`.

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/Screen screen` | the `https://developer.android.com/reference/androidx/car/app/Screen` to push on top of the stack |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/OnScreenResultListener onScreenResultListener` | the listener that will be executed with the result pushed by the `screen` through `https://developer.android.com/reference/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)

```
public void remove(@NonNull Screen screen)
```

Removes the `screen` from the stack.

If the `screen` is the only `https://developer.android.com/reference/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/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 |