Session

The Session provides the primary interface to spatialized functionality for the application. Each spatialized Activity must create and hold an instance of Session. Once created, the application can use the Session interfaces to create spatialized content entities such as panels or 3d models, as well as set a spatial environment, identify user position, and anchor content to the real world.

Access Session from Jetpack Compose for XR

When using Jetpack Compose for XR, the session is created for you and can be accessed using LocalSession.current. See the following example:

@Composable
fun ComposableUsingSession() {
    val session = LocalSession.current
}

Access Session from Jetpack SceneCore

If you are creating spatialized entities from the SceneCore library, you'll need to create the session.

Creating a session is only supported on an Android XR device. Attempting to create a session on an incompatible device will result in a failed result. To create a session, pass an activity to the create method, as shown in the following example.

when (val result = Session.create(this)) {
    is SessionCreateSuccess -> {
        val xrSession = result.session
        // ...
    }
    is SessionCreatePermissionsNotGranted ->
        TODO(/* The required permissions in result.permissions have not been granted. */)
}

When a session's activity is destroyed, all spatial UI and 3D content associated with that session is destroyed and the session is no longer valid.

See also