setBackHandler

fun View.setBackHandler(enabled: Boolean = true, onBack: () -> Unit)

A function to be called if the device back button is pressed while this view is active, as determined by its ViewTreeLifecycleOwner, via an OnBackPressedCallback. On succeeding calls, the previously created OnBackPressedCallback will be updated, and will maintain its position in the OnBackPressedDispatcher priority queue.

Parameters

enabled
onBack

Wrapper for the two arg variant of setBackHandler, a convenience for the common pattern of using a nullable function as the back handler to indicate that back handling should be disabled.

Parameters

onBack

the handler function to run when the device back button is tapped / back gesture is made. If null, the relevant OnBackPressedCallback will be disabled, but it will still exist -- this View's priority in the OnBackPressedDispatcher queue will not change, should a non-null handler be provided by a later call.