# Session

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/Session.java+class:androidx.car.app.Session) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

*** ** * ** ***


```
public abstract class Session implements LifecycleOwner
```

<br />

*** ** * ** ***

The base class for implementing a session for a car app.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/car/app/Session#Session()()` |

| ### Public methods |
|---|---|
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/CarContext` | `https://developer.android.com/reference/androidx/car/app/Session#getCarContext()()` Returns the `https://developer.android.com/reference/androidx/car/app/CarContext` for this session. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/Lifecycle` | `https://developer.android.com/reference/androidx/car/app/Session#getLifecycle()()` Returns the `https://developer.android.com/reference/androidx/car/app/Session`'s `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`. |
| `void` | `https://developer.android.com/reference/androidx/car/app/Session#onCarConfigurationChanged(android.content.res.Configuration)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Configuration.html newConfiguration)` Notifies that the `https://developer.android.com/reference/androidx/car/app/CarContext`'s `https://developer.android.com/reference/android/content/res/Configuration.html` has changed. |
| `abstract @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/Session#onCreateScreen(android.content.Intent)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html intent)` Requests the first `https://developer.android.com/reference/androidx/car/app/Screen` for the application. |
| `void` | `https://developer.android.com/reference/androidx/car/app/Session#onNewIntent(android.content.Intent)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html intent)` Notifies that the car app has received a new `https://developer.android.com/reference/android/content/Intent.html`. |

## Public constructors

### Session

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

```
public Session()
```

## Public methods

### getCarContext

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

```
public final @NonNull CarContext getCarContext()
```

Returns the `https://developer.android.com/reference/androidx/car/app/CarContext` for this session.

**The `https://developer.android.com/reference/androidx/car/app/CarContext` is not expected to be available until this session's `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` is at least `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#CREATED`** . Further, some instance methods within `https://developer.android.com/reference/androidx/car/app/CarContext` should not be called before this state has been reached. See the documentation in `https://developer.android.com/reference/androidx/car/app/CarContext` for details on any such restrictions.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/Session#getLifecycle()` |   |

### getLifecycle

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

```
public @NonNull Lifecycle getLifecycle()
```

Returns the `https://developer.android.com/reference/androidx/car/app/Session`'s `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`.

Here are some of the ways you can use the sessions's `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`:

- Observe its `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` by calling `https://developer.android.com/reference/androidx/lifecycle/Lifecycle#addObserver(androidx.lifecycle.LifecycleObserver)`. You can use the `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver` to take specific actions whenever the `https://developer.android.com/reference/androidx/car/app/Screen` receives different `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event`s.
- Use this `https://developer.android.com/reference/androidx/car/app/CarAppService` to observe `https://developer.android.com/reference/androidx/lifecycle/LiveData`s that may drive the backing data for your application.

What each lifecycle related event means for a session:

`https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_CREATE`

:   The session has just been launched, and this session is being initialized. `https://developer.android.com/reference/androidx/car/app/Session#onCreateScreen(android.content.Intent)` will be called at a point after this call.

`https://developer.android.com/reference/androidx/car/app/Session#onCreateScreen(android.content.Intent)`

:   The host is ready for this session to create the first `https://developer.android.com/reference/androidx/car/app/Screen` so that it can display its template.

`https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_START`

:   The application is now visible in the car screen.

`https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_RESUME`

:   The user can now interact with this application.

`https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_PAUSE`

:   The user can no longer interact with this application.

`https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_STOP`

:   The application is no longer visible.

`https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_DESTROY`

:   The OS has now destroyed this `https://developer.android.com/reference/androidx/car/app/Session` instance, and it is no longer valid.

Listeners that are added in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_START`, should be removed in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_STOP`.

Listeners that are added in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_CREATE` should be removed in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_DESTROY`.

Note lifecycle callbacks will be executed on the main thread.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver` |   |

### onCarConfigurationChanged

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

```
public void onCarConfigurationChanged(@NonNull Configuration newConfiguration)
```

Notifies that the `https://developer.android.com/reference/androidx/car/app/CarContext`'s `https://developer.android.com/reference/android/content/res/Configuration.html` has changed.

At the time that this function is called, the `https://developer.android.com/reference/androidx/car/app/CarContext`'s resources object will have been updated to return resource values matching the new configuration.

Called by the system, do not call this method directly.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/CarContext` |   |

### onCreateScreen

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

```
public abstract @NonNull Screen onCreateScreen(@NonNull Intent intent)
```

Requests the first `https://developer.android.com/reference/androidx/car/app/Screen` for the application.

Once the method returns, `https://developer.android.com/reference/androidx/car/app/Screen#onGetTemplate()` will be called on the `https://developer.android.com/reference/androidx/car/app/Screen` returned, and the app will be displayed on the car screen.

To pre-seed a back stack, you can push `https://developer.android.com/reference/androidx/car/app/Screen`s onto the stack, via `https://developer.android.com/reference/androidx/car/app/ScreenManager#push(androidx.car.app.Screen)` during this method call.

Called by the system, do not call this method directly.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html intent` | the intent that was used to start this app. If the app was started with a call to `https://developer.android.com/reference/androidx/car/app/CarContext#startCarApp(android.content.Intent)`, this intent will be equal to the intent passed to that method |

### onNewIntent

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

```
public void onNewIntent(@NonNull Intent intent)
```

Notifies that the car app has received a new `https://developer.android.com/reference/android/content/Intent.html`.

Once the method returns, `https://developer.android.com/reference/androidx/car/app/Screen#onGetTemplate()` will be called on the `https://developer.android.com/reference/androidx/car/app/Screen` that is on top of the `https://developer.android.com/reference/androidx/car/app/Screen` stack managed by the `https://developer.android.com/reference/androidx/car/app/ScreenManager`, and the app will be displayed on the car screen.

Often used to update the current `https://developer.android.com/reference/androidx/car/app/Screen` or pushing a new one on the stack, based off of the information in the `intent`.

Called by the system, do not call this method directly.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html intent` | the intent that was used to start this app. If the app was started with a call to `https://developer.android.com/reference/androidx/car/app/CarContext#startCarApp(android.content.Intent)`, this intent will be equal to the intent passed to that method |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/CarContext#startCarApp(android.content.Intent)` |   |