Objects implementing this interface are notified when they are initially used in a composition and when they are no longer being used.
An object is remembered by the composition if it is remembered in at least one place in a composition. Compose may implicitly remember an object if doing so is required to restart the composition later, such as for composable function parameters. An object is forgotten when it is no longer remembered anywhere in that composition. If a single instance is remembered in more than one location in the same composition, its onRemembered and onForgotten will be called for each location in the composition.
When objects implementing this interface is remembered and forgotten together, the order of onForgotten is guaranteed to be called in the opposite order of onRemembered. For example, if two objects, A and B are remembered together, A followed by B, onRemembered will be called first on A then on B. If they forgotten together then onForgotten will be called on B first then on A.
Implementations of RememberObserver should generally not expose those object references outside of their immediate usage sites. A RememberObserver reference that is propagated to multiple parts of a composition might remain present in the composition for longer than expected if it is remembered (explicitly or implicitly) elsewhere, and a RememberObserver that appears more once can have its callback methods called multiple times in no meaningful order and on multiple threads.
An object remembered in only one place in only one composition is guaranteed to,
Called when this object is successfully remembered by a composition. This method is called on the composition's apply thread.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-05-15 UTC.
[null,null,["Last updated 2025-05-15 UTC."],[],[],null,["# RememberObserver\n================\n\nArtifact: [androidx.compose.runtime:runtime](/jetpack/androidx/releases/compose-runtime) \n[View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/RememberObserver.kt+class:androidx.compose.runtime.RememberObserver) \nAdded in [1.0.0](/jetpack/androidx/releases/compose-runtime#1.0.0)\nCommon/AllAndroid/JVMNative/C/iOSJavaScript\n - Cmn \n\n ```\n interface RememberObserver\n ```\n\n*** ** * ** ***\n\nObjects implementing this interface are notified when they are initially used in a composition and when they are no longer being used.\n\nAn object is [remembered](/reference/kotlin/androidx/compose/runtime/RememberObserver#onRemembered()) by the composition if it is [remember](/reference/kotlin/androidx/compose/runtime/package-summary#remember(kotlin.Function0))ed in at least one place in a composition. Compose may implicitly [remember](/reference/kotlin/androidx/compose/runtime/package-summary#remember(kotlin.Function0)) an object if doing so is required to restart the composition later, such as for composable function parameters. An object is [forgotten](/reference/kotlin/androidx/compose/runtime/RememberObserver#onForgotten()) when it is no longer [remember](/reference/kotlin/androidx/compose/runtime/package-summary#remember(kotlin.Function0))ed anywhere in that composition. If a single instance is [remember](/reference/kotlin/androidx/compose/runtime/package-summary#remember(kotlin.Function0))ed in more than one location in the same composition, its [onRemembered](/reference/kotlin/androidx/compose/runtime/RememberObserver#onRemembered()) and [onForgotten](/reference/kotlin/androidx/compose/runtime/RememberObserver#onForgotten()) will be called for each location in the composition.\n\nWhen objects implementing this interface is remembered and forgotten together, the order of [onForgotten](/reference/kotlin/androidx/compose/runtime/RememberObserver#onForgotten()) is guaranteed to be called in the opposite order of [onRemembered](/reference/kotlin/androidx/compose/runtime/RememberObserver#onRemembered()). For example, if two objects, A and B are [remember](/reference/kotlin/androidx/compose/runtime/package-summary#remember(kotlin.Function0))ed together, A followed by B, [onRemembered](/reference/kotlin/androidx/compose/runtime/RememberObserver#onRemembered()) will be called first on A then on B. If they forgotten together then [onForgotten](/reference/kotlin/androidx/compose/runtime/RememberObserver#onForgotten()) will be called on B first then on A.\n\nImplementations of [RememberObserver](/reference/kotlin/androidx/compose/runtime/RememberObserver) should generally not expose those object references outside of their immediate usage sites. A [RememberObserver](/reference/kotlin/androidx/compose/runtime/RememberObserver) reference that is propagated to multiple parts of a composition might remain present in the composition for longer than expected if it is remembered (explicitly or implicitly) elsewhere, and a [RememberObserver](/reference/kotlin/androidx/compose/runtime/RememberObserver) that appears more once can have its callback methods called multiple times in no meaningful order and on multiple threads.\n\nAn object remembered in only one place in only one composition is guaranteed to,\n\n1. have either [onRemembered](/reference/kotlin/androidx/compose/runtime/RememberObserver#onRemembered()) or [onAbandoned](/reference/kotlin/androidx/compose/runtime/RememberObserver#onAbandoned()) called\n\n2. if [onRemembered](/reference/kotlin/androidx/compose/runtime/RememberObserver#onRemembered()) is called, [onForgotten](/reference/kotlin/androidx/compose/runtime/RememberObserver#onForgotten()) will eventually be called\n\nSummary\n-------\n\n| ### Public functions |\n|-------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|\n| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onAbandoned](/reference/kotlin/androidx/compose/runtime/RememberObserver#onAbandoned())`()` Called when this object is returned by the callback to `remember` but is not successfully remembered by a composition. | Cmn |\n| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onForgotten](/reference/kotlin/androidx/compose/runtime/RememberObserver#onForgotten())`()` Called when this object is forgotten by a composition. | Cmn |\n| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onRemembered](/reference/kotlin/androidx/compose/runtime/RememberObserver#onRemembered())`()` Called when this object is successfully remembered by a composition. | Cmn |\n\nPublic functions\n----------------\n\n### onAbandoned\n\nCmn \nAdded in [1.0.0](/jetpack/androidx/releases/compose-runtime#1.0.0) \n\n```\nfun onAbandoned(): Unit\n```\n\nCalled when this object is returned by the callback to `remember` but is not successfully remembered by a composition. \n\n### onForgotten\n\nCmn \nAdded in [1.0.0](/jetpack/androidx/releases/compose-runtime#1.0.0) \n\n```\nfun onForgotten(): Unit\n```\n\nCalled when this object is forgotten by a composition. This method is called on the composition's **apply thread.** \n\n### onRemembered\n\nCmn \nAdded in [1.0.0](/jetpack/androidx/releases/compose-runtime#1.0.0) \n\n```\nfun onRemembered(): Unit\n```\n\nCalled when this object is successfully remembered by a composition. This method is called on the composition's **apply thread.**"]]