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 a Context to the create() method. This Context can be an Activity on immersive XR devices or mobile devices, or a projected context on Intelligent Eyewear.

Creating an ARCore session can be an expensive task, and should be processed in the background, as shown in the following example:

lifecycleScope.launch {
    when (val result = Session.create(context)) {
        is SessionCreateSuccess -> {
            val xrSession = result.session
            // ...
        }
        else ->
            TODO(/* A different unhandled exception was thrown. */)
    }
}

The create() function can use an optional LifecycleOwner parameter, which allows the session's lifecycle to be managed by a different LifecycleOwner. The LifecycleOwner must begin and end its lifecycle within the scope of the provided context, otherwise the Session may be prone to leaks.

See also