valnewConfig=session.config.copy(anchorPersistence=Config.AnchorPersistenceMode.LOCAL,)when(valresult=session.configure(newConfig)){isSessionConfigureConfigurationNotSupported->
TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)isSessionConfigureSuccess->TODO(/* Success! */)else->
TODO(/* A different unhandled exception was thrown. */)}
when(valresult=Anchor.load(session,uuid)){isAnchorCreateSuccess->{// Loading was successful. The anchor is stored in result.anchor.}else->{// handle failure}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-30。"],[],[],null,["An Anchor describes a fixed location and orientation in the real world.\nAttaching an object to an anchor helps objects appear realistically placed in\nthe real world.\n\nCreate an ARCore for Jetpack XR session\n\nCreate anchors through an ARCore for Jetpack XR session. See [Understand a\nSession's lifecycle](/develop/xr/jetpack-xr-sdk/work-with-arcore#session-lifecycle) to obtain a [`Session`](/reference/kotlin/androidx/xr/runtime/Session).\n\nConfigure the Session\n\nCreating and loading anchors does not require the session to be configured.\nHowever, anchor persistence is not enabled by default on XR sessions. To persist\nand load anchors from local storage, configure the session and set the\n[`AnchorPersistenceMode.LOCAL`](/reference/kotlin/androidx/xr/runtime/Config.AnchorPersistenceMode#LOCAL()) mode:\n\n\n```kotlin\nval newConfig = session.config.copy(\n anchorPersistence = Config.AnchorPersistenceMode.LOCAL,\n)\nwhen (val result = session.configure(newConfig)) {\n is SessionConfigureConfigurationNotSupported -\u003e\n TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)\n is SessionConfigureSuccess -\u003e TODO(/* Success! */)\n else -\u003e\n TODO(/* A different unhandled exception was thrown. */)\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L33-L42\n```\n\n\u003cbr /\u003e\n\n| **Note:** Creating, loading, and persisting anchors requires the `android.permission.SCENE_UNDERSTANDING_COARSE` [runtime permission](/training/permissions/requesting) to be granted to your app.\n\nAnchor content to a fixed location in space\n\nAn anchor is created using a [`Pose`](/reference/kotlin/androidx/xr/runtime/math/Pose), which can be interpreted relative to\nan existing [`Trackable`](/reference/kotlin/androidx/xr/arcore/Trackable) or not.\n\nCreate an anchor relative to a Trackable\n\nWhen an anchor is created relative to a `Trackable`, such as a `Plane`, which\nmakes the anchor follow the attached `Trackable` when it moves through space.\n\n\n```kotlin\nwhen (val result = trackable.createAnchor(pose)) {\n is AnchorCreateSuccess -\u003e { /* anchor stored in `result.anchor`. */ }\n else -\u003e { /* handle failure */ }\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L59-L62\n```\n\n\u003cbr /\u003e\n\nCreate an anchor without a Trackable\n\nTo create an anchor that isn't attached to a `Trackable`:\n\n\n```kotlin\nwhen (val result = Anchor.create(session, pose)) {\n is AnchorCreateSuccess -\u003e { /* anchor stored in `result.anchor`. */ }\n else -\u003e { /* handle failure */ }\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L49-L52\n```\n\n\u003cbr /\u003e\n\nAttach an entity to an anchor\n\nTo render a model at this location, [create a `GltfModel`](/develop/xr/jetpack-xr-sdk/add-3d-models#place-3d) and set its parent\nto an `AnchorEntity`.\n\n\n```kotlin\nAnchorEntity.create(session, anchor).apply {\n parent = session.scene.activitySpace\n addChild(entity)\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L72-L75\n```\n\n\u003cbr /\u003e\n\nUnderstand TrackingState\n\nEach `Trackable` has a `TrackingState` that should be checked before being used.\nA `Trackable` that has a `TrackableState` of `Tracking` has its `Pose` actively\nupdated by the system. A `Trackable` that is `Paused` may become `Tracking` in\nthe future, whereas one that is `Stopped` will never become `Tracking`.\n\nPersist an Anchor throughout sessions\n\nAn anchor that is not persisted disappears after a session is destroyed. By\npersisting an anchor, your app remembers that anchor's position in its private\napp data. This anchor can be retrieved in a subsequent session and is anchored\nin the same location in the world.\n\nTo persist an anchor, use [`Anchor.persist()`](/reference/kotlin/androidx/xr/arcore/Anchor#persist) as shown here:\n\n\n```kotlin\nval uuid = anchor.persist()https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L26-L26\n```\n\n\u003cbr /\u003e\n\nYour app can retrieve the anchor by using the [`UUID`](/reference/java/util/UUID) in a future session:\n\n\n```kotlin\nwhen (val result = Anchor.load(session, uuid)) {\n is AnchorCreateSuccess -\u003e {\n // Loading was successful. The anchor is stored in result.anchor.\n }\n else -\u003e {\n // handle failure\n }\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L32-L39\n```\n\n\u003cbr /\u003e\n\nWhen you don't need an anchor anymore, call [`unpersist()`](/reference/kotlin/androidx/xr/arcore/Anchor#unpersist(androidx.xr.runtime.Session,java.util.UUID)). This removes\nthe anchor from your app's storage and makes the given UUID unretrievable for\ncalls to [`Anchor.load()`](/reference/kotlin/androidx/xr/arcore/Anchor#load).\n\n\n```kotlin\nAnchor.unpersist(session, uuid)https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L45-L45\n```\n\n\u003cbr /\u003e\n\nYour app can also request a list of all anchors that have been persisted that\nare still present in your app's storage:\n\n\n```kotlin\nval uuids = Anchor.getPersistedAnchorUuids(session)https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L51-L51\n```\n\n\u003cbr /\u003e"]]