invokeOnRemoval

Register a callback to be invoked when this entry is removed from the backstack.

When Handlers Are Called

Removal handlers execute in two scenarios:

  1. Normal removal - When navigating back/away and the entry leaves the backstack. An entry with an exit transition runs them once that transition has finished

  2. Store reset - When Store.reset() is called, for every entry that has a lifecycle, including one that is still leaving

Each lifecycle runs its handlers exactly once. On normal removal they run before the lifecycle scope is cancelled, so work launched on the lifecycle still runs. On reset the scope has already been cancelled, so launch on the lifecycle does nothing, while StoreAccessor.launch runs in the new generation. Multiple handlers can be registered and all will execute.

Handler Execution

  • Handlers are non-suspend functions (synchronous)

  • Use StoreAccessor.launch for suspend/async work (fire-and-forget)

  • The RemovalReason parameter indicates why the entry is being removed

  • Handlers receive StoreAccessor as receiver for dispatch, state access, etc.

Example

lifecycle.invokeOnRemoval { reason ->
// `this` is StoreAccessor, reason indicates NAVIGATION or RESET
if (reason == RemovalReason.NAVIGATION) {
launch {
val logic = selectLogic<SomeLogic>()
logic.cleanup()
}
}
}

Parameters

handler

Callback invoked with StoreAccessor as receiver and RemovalReason as parameter.