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:
import androidx.xr.compose.platform.LocalSession
class myActivity : Activity() {
val xrSession = checkNotNull(LocalSession.current)
}
Access Session from Jetpack SceneCore
If you are creating spatialized entities from the SceneCore library, you'll need to instantiate the session yourself.
Creating a session is only supported on an Android XR device. At this time, if
you create a Session
and try to use it on a non Android XR device, you'll get a
NullPointerException
.
You can create only one session per activity. To create a session, pass an
activity to the create
method, as shown in the following example.
Attempting to create a session on an incompatible device may throw runtime
errors.
import androidx.xr.scenecore.Session
class myActivity : Activity() {
val xrSession = Session.create(this)
}
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
- Check for spatial capabilities
- Transition between HSM and FSM
- Add spatial environments to your app
- Add 3D models to your app