# LifecycleRegistry

Artifact: [androidx.lifecycle:lifecycle-runtime](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/LifecycleRegistry.kt+class:androidx.lifecycle.LifecycleRegistry) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

*** ** * ** ***


```
public class LifecycleRegistry extends Lifecycle
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.lifecycle.Lifecycle](https://developer.android.com/reference/androidx/lifecycle/Lifecycle) ||
|   | ↳ | [androidx.lifecycle.LifecycleRegistry](https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry) |

*** ** * ** ***

A `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` implementation that manages multiple `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s.

Commonly used by UI containers (like Activities or Fragments on Android) to manage component lifecycles. Can be used directly to implement custom `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` components.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#LifecycleRegistry(androidx.lifecycle.LifecycleOwner)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner provider)` Creates a new `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry` for the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#(androidx.lifecycle.LifecycleOwner)`. |

| ### Public methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#addObserver(androidx.lifecycle.LifecycleObserver)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver observer)` Adds a `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver` that will be notified when the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` changes state. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#createUnsafe(androidx.lifecycle.LifecycleOwner)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner)` Creates a new `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry` for the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` without main-thread enforcement. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` | `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#getCurrentState()()` The current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State>` | `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#getCurrentStateFlow()()` Returns a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` where the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/value.html` represents the current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of this `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`. |
| `int` | `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#getObserverCount()()` The number of registered `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s. |
| `void` | `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event event)` Sets the current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` and notifies the `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/MainThread [markState](https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#markState(androidx.lifecycle.Lifecycle.State))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state)` **This method is deprecated.** Override \[currentState\]. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#removeObserver(androidx.lifecycle.LifecycleObserver)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver observer)` Removes the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#removeObserver(androidx.lifecycle.LifecycleObserver)` from the list of registered `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s. |
| `void` | `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#setCurrentState(androidx.lifecycle.Lifecycle.State)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state)` The current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`. |

## Public constructors

### LifecycleRegistry

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
public LifecycleRegistry(@NonNull LifecycleOwner provider)
```

Creates a new `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry` for the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#(androidx.lifecycle.LifecycleOwner)`.

You should usually create this inside your `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` class's constructor and hold onto the same instance.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner provider` | The owner `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` |

## Public methods

### addObserver

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
@MainThread
public void addObserver(@NonNull LifecycleObserver observer)
```

Adds a `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver` that will be notified when the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` changes state.

The given `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#addObserver(androidx.lifecycle.LifecycleObserver)` will be brought to the current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`. For example, if the `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` is in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED`, the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#addObserver(androidx.lifecycle.LifecycleObserver)` receives `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_CREATE` and `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event`s.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver observer` | The `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver` to notify. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if no event exists to move up from the observer's initial state. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if main-thread enforcement is enabled and called on a thread other than the main thread. |

### createUnsafe

Added in [2.6.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.6.0)

```
@VisibleForTesting
public static final @NonNull LifecycleRegistry createUnsafe(@NonNull LifecycleOwner owner)
```

Creates a new `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry` for the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` without main-thread enforcement.

Note: `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry` is not thread-safe. Multiple threads accessing it must synchronize externally. Useful for JVM testing where a main thread dispatcher is absent.

### getCurrentState

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
public @NonNull Lifecycle.State getCurrentState()
```

The current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`.

Transitions the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` to the given target `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` and dispatches the corresponding `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event`s to any registered `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if main-thread enforcement is enabled and called on a thread other than the main thread. |

### getCurrentStateFlow

```
public @NonNull StateFlow<@NonNull Lifecycle.State> getCurrentStateFlow()
```

Returns a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` where the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/value.html` represents the current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of this `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`.

### getObserverCount

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
public int getObserverCount()
```

The number of registered `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if main-thread enforcement is enabled and called on a thread other than the main thread. |

### handleLifecycleEvent

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
public void handleLifecycleEvent(@NonNull Lifecycle.Event event)
```

Sets the current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` and notifies the `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s.

No-ops if the target state of the `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)` matches `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#currentState()`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event event` | The `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event` to process. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if main-thread enforcement is enabled and called on a thread other than the main thread. |

### markState

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0) Deprecated in [2.1.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.1.0)

```
@MainThread
public void [markState](https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#markState(androidx.lifecycle.Lifecycle.State))(@NonNull Lifecycle.State state)
```

> [!CAUTION]
> **This method is deprecated.**   
> Override \[currentState\].

Moves the Lifecycle to the given state and dispatches necessary events to the observers.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state` | new state |

### removeObserver

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
@MainThread
public void removeObserver(@NonNull LifecycleObserver observer)
```

Removes the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleRegistry#removeObserver(androidx.lifecycle.LifecycleObserver)` from the list of registered `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver observer` | The `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver` to remove. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if main-thread enforcement is enabled and called on a thread other than the main thread. |

### setCurrentState

Added in [2.1.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.1.0)

```
public void setCurrentState(@NonNull Lifecycle.State state)
```

The current `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` of the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle`.

Transitions the `https://developer.android.com/reference/androidx/lifecycle/Lifecycle` to the given target `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` and dispatches the corresponding `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event`s to any registered `https://developer.android.com/reference/androidx/lifecycle/LifecycleObserver`s.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if main-thread enforcement is enabled and called on a thread other than the main thread. |