I’m not sure how to refactor this code into the ne...
# compose
o
I’m not sure how to refactor this code into the new SideEffect API’s which replace onCommit/onDispose as of alpha11
Copy code
@Composable
internal fun Handler(
    enabled: Boolean = true,
    onBackPressed: () -> Unit
) {
    val dispatcher =
        (AmbientBackPressedDispatcher.current ?: return).onBackPressedDispatcher

    val handler = remember { ComposableBackHandler(enabled) }

    onCommit(dispatcher) {
        dispatcher.addCallback(handler)

        onDispose { handler.remove() }
    }
    onCommit(enabled) {
        handler.isEnabled = enabled
        handler.onBackPressed = onBackPressed
    }
}
the
onCommit
and
onDispose
methods are now unresolved, not sure what to replace them with or how
i
Note that
BackHandler
is a part of the
activity-compose
artifact, you don't need to write this code at all anymore: https://developer.android.com/reference/kotlin/androidx/activity/compose/package-summary#backhandler
It uses
by rememberUpdatedState
for the callback and
SideEffect
for the
enabled
state update
o
aah, thank you guys
i
And
DisposableEffect
for the `addCallback`/`remove` pair
n
Thanks @Ian Lake! I think a lot of people is using this BackButtonHandler snippet 😅