An Anchor describes a fixed location and orientation in the real world. Attaching an object to an anchor helps objects appear realistically placed in the real world.

## Create an ARCore for Jetpack XR session

Create anchors through an ARCore for Jetpack XR session. See[Understand a Session's lifecycle](https://developer.android.com/develop/xr/jetpack-xr-sdk/work-with-arcore#session-lifecycle)to obtain a[`Session`](https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session).

## Configure the Session

Creating and loading anchors does not require the session to be configured. However, anchor persistence is not enabled by default on XR sessions. To persist and load anchors from local storage, configure the session and set the[`AnchorPersistenceMode.LOCAL`](https://developer.android.com/reference/kotlin/androidx/xr/runtime/Config.AnchorPersistenceMode#LOCAL())mode:

<br />

```kotlin
val newConfig = session.config.copy(
    anchorPersistence = Config.AnchorPersistenceMode.LOCAL,
)
when (val result = session.configure(newConfig)) {
    is SessionConfigureSuccess -> TODO(/* Success! */)
    else ->
        TODO(/* The session could not be configured. See SessionConfigureResult for possible causes. */)
}https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L32-L39
```

<br />

| **Note:** Creating, loading, and persisting anchors requires the`android.permission.SCENE_UNDERSTANDING_COARSE`[runtime permission](https://developer.android.com/training/permissions/requesting)to be granted to your app.

## Anchor content to a fixed location in space

An anchor is created using a[`Pose`](https://developer.android.com/reference/kotlin/androidx/xr/runtime/math/Pose), which can be interpreted relative to an existing[`Trackable`](https://developer.android.com/reference/kotlin/androidx/xr/arcore/Trackable)or not.

### Create an anchor relative to a Trackable

When an anchor is created relative to a`Trackable`, such as a`Plane`, which makes the anchor follow the attached`Trackable`when it moves through space.

<br />

```kotlin
when (val result = trackable.createAnchor(pose)) {
    is AnchorCreateSuccess -> { /* anchor stored in `result.anchor`. */ }
    else -> { /* handle failure */ }
}https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L56-L59
```

<br />

### Create an anchor without a Trackable

To create an anchor that isn't attached to a`Trackable`:

<br />

```kotlin
when (val result = Anchor.create(session, pose)) {
    is AnchorCreateSuccess -> { /* anchor stored in `result.anchor`. */ }
    else -> { /* handle failure */ }
}https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L46-L49
```

<br />

### Attach an entity to an anchor

To render a model at this location,[create a`GltfModel`](https://developer.android.com/develop/xr/jetpack-xr-sdk/add-3d-models#place-3d)and set its parent to an`AnchorEntity`.

<br />

```kotlin
AnchorEntity.create(session, anchor).apply {
    parent = session.scene.activitySpace
    addChild(entity)
}https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/Anchors.kt#L69-L72
```

<br />

## Understand TrackingState

Each`Trackable`has a`TrackingState`that should be checked before being used. A`Trackable`that has a`TrackableState`of`Tracking`has its`Pose`actively updated by the system. A`Trackable`that is`Paused`may become`Tracking`in the future, whereas one that is`Stopped`will never become`Tracking`.

## Persist an Anchor throughout sessions

An anchor that is not persisted disappears after a session is destroyed. By persisting an anchor, your app remembers that anchor's position in its private app data. This anchor can be retrieved in a subsequent session and is anchored in the same location in the world.

To persist an anchor, use[`Anchor.persist()`](https://developer.android.com/reference/kotlin/androidx/xr/arcore/Anchor#persist)as shown here:

<br />

```kotlin
val uuid = anchor.persist()https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L26-L26
```

<br />

Your app can retrieve the anchor by using the[`UUID`](https://developer.android.com/reference/java/util/UUID)in a future session:

<br />

```kotlin
when (val result = Anchor.load(session, uuid)) {
    is AnchorCreateSuccess -> {
        // Loading was successful. The anchor is stored in result.anchor.
    }
    else -> {
        // handle failure
    }
}https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L32-L39
```

<br />

When you don't need an anchor anymore, call[`unpersist()`](https://developer.android.com/reference/kotlin/androidx/xr/arcore/Anchor#unpersist(androidx.xr.runtime.Session,java.util.UUID)). This removes the anchor from your app's storage and makes the given UUID unretrievable for calls to[`Anchor.load()`](https://developer.android.com/reference/kotlin/androidx/xr/arcore/Anchor#load).

<br />

```kotlin
Anchor.unpersist(session, uuid)https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L45-L45
```

<br />

Your app can also request a list of all anchors that have been persisted that are still present in your app's storage:

<br />

```kotlin
val uuids = Anchor.getPersistedAnchorUuids(session)https://github.com/android/snippets/blob/95aeebd507b29719a9e7d5a839f101bbbe42ea72/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt#L51-L51
```

<br />