Access a session for creating spatialized UI and entities

Applicable XR devices
This guidance helps you build experiences for these types of XR devices.
XR Headsets
Wired XR Glasses

The Session provides the primary interface to spatialized functionality for your app. Each spatialized Activity must create and hold an instance of Session. After your app creates a session, it 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 a 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 a session from Jetpack XR Runtime

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

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
        // ...
    }
    else ->
        TODO(/* A different unhandled exception was thrown. */)
}

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