when(valresult=Session.create(this)){isSessionCreateSuccess->{valxrSession=result.session// ...}else->
TODO(/* A different unhandled exception was thrown. */)}
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# Session\n\nThe [`Session`](/reference/kotlin/androidx/xr/runtime/Session) provides the primary interface to spatialized functionality\nfor the application. Each spatialized Activity must create and hold an instance\nof `Session`. Once created, the application can use the `Session` interfaces to\ncreate spatialized content entities such as panels or 3d models, as well as [set\na spatial environment](/develop/xr/jetpack-xr-sdk/add-environments), [identify user position](/reference/kotlin/androidx/xr/scenecore/SpatialUser), and [anchor content](/develop/xr/jetpack-xr-sdk/work-with-entities)\nto the real world.\n| **Caution:** Due to a [known issue](/jetpack/androidx/releases/xr-scenecore#1.0.0-alpha04) that ties the session to the Activity lifecycle, the session can become invalid in various situations that automatically recreate the activity. These include, but are not limited to resizing a main panel, connecting peripherals, and changing between light and dark theme. If you run into session invalidation issues, you may need to make your main panel non-resizable, use a dynamic panel entity, disable activity recreation for [specific config changes](/guide/topics/resources/runtime-changes#restrict-activity), or disable light or dark theme changes.\n\nAccess Session from Jetpack Compose for XR\n------------------------------------------\n\nWhen using Jetpack Compose for XR, the session is created for you and can be\naccessed using [`LocalSession.current`](/reference/kotlin/androidx/xr/compose/platform/package-summary#LocalSession()). See the following example:\n\n\n```kotlin\n@Composable\nfun ComposableUsingSession() {\n val session = LocalSession.current\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/xr/src/main/java/com/example/xr/runtime/Session.kt#L26-L29\n```\n\n\u003cbr /\u003e\n\nAccess Session from Jetpack SceneCore\n-------------------------------------\n\nIf you are creating spatialized entities from the SceneCore library, you'll need\nto create the session.\n\nCreating a session is only supported on an Android XR device. Attempting to\ncreate a session on an incompatible device will result in a failed result. To\ncreate a session, pass an activity to the [`create()`](/reference/kotlin/androidx/xr/runtime/Session#create(android.app.Activity,kotlin.coroutines.CoroutineContext)) method, as shown in\nthe following example.\n\n\n```kotlin\nwhen (val result = Session.create(this)) {\n is SessionCreateSuccess -\u003e {\n val xrSession = result.session\n // ...\n }\n else -\u003e\n TODO(/* A different unhandled exception was thrown. */)\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/xr/src/main/java/com/example/xr/runtime/Session.kt#L34-L41\n```\n\n\u003cbr /\u003e\n\n| **Note:** Some features, such as [hand tracking](/develop/xr/jetpack-xr-sdk/arcore/hands) and [plane tracking](/develop/xr/jetpack-xr-sdk/arcore/planes), require additional runtime permissions in order for session configuration to succeed.\n\nWhen a session's activity is destroyed, all spatial UI and 3D content associated\nwith that session is destroyed and the session is no longer valid.\n\nSee also\n--------\n\n- [Check for spatial capabilities](/develop/xr/jetpack-xr-sdk/check-spatial-capabilities)\n- [Transition between HSM and FSM](/develop/xr/jetpack-xr-sdk/transition-home-space-to-full-space)\n- [Add spatial environments to your app](/develop/xr/jetpack-xr-sdk/add-environments)\n- [Add 3D models to your app](/develop/xr/jetpack-xr-sdk/add-3d-models)"]]