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:
Normal removal - When navigating back/away and the entry leaves the backstack
Store reset - When
Store.reset()is called (for all entries in backstack)
Handlers run before the lifecycle scope is cancelled, so the store is still fully operational. 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
Callback invoked with StoreAccessor as receiver and RemovalReason as parameter.