OnBackPressedCallback


abstract class OnBackPressedCallback


Class for handling OnBackPressedDispatcher.onBackPressed callbacks without strongly coupling that implementation to a subclass of ComponentActivity.

This class maintains its own enabled state. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.

Note that the enabled state is an additional layer on top of the androidx.lifecycle.LifecycleOwner passed to OnBackPressedDispatcher.addCallback which controls when the callback is added and removed to the dispatcher.

By calling remove, this callback will be removed from any OnBackPressedDispatcher it has been added to. It is strongly recommended to instead disable this callback to handle temporary changes in state.

Summary

Public constructors

Public functions

open Unit

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackCancelled.

abstract Unit

Callback for handling the OnBackPressedDispatcher.onBackPressed event.

open Unit

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackProgressed.

open Unit

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackStarted.

Unit

Removes this callback from any OnBackPressedDispatcher it is currently added to.

Public properties

Boolean

The enabled state of the callback.

Public constructors

OnBackPressedCallback

Added in 1.0.0
OnBackPressedCallback(enabled: Boolean)
Parameters
enabled: Boolean

The default enabled state for this callback.

Public functions

handleOnBackCancelled

Added in 1.8.0
@MainThread
open fun handleOnBackCancelled(): Unit

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackCancelled.

This will only be called by the framework on API 34 and above.

handleOnBackPressed

Added in 1.0.0
@MainThread
abstract fun handleOnBackPressed(): Unit

Callback for handling the OnBackPressedDispatcher.onBackPressed event.

handleOnBackProgressed

Added in 1.8.0
@MainThread
open fun handleOnBackProgressed(backEvent: BackEventCompat): Unit

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackProgressed.

This will only be called by the framework on API 34 and above.

handleOnBackStarted

Added in 1.8.0
@MainThread
open fun handleOnBackStarted(backEvent: BackEventCompat): Unit

Callback for handling the system UI generated equivalent to OnBackPressedDispatcher.dispatchOnBackStarted.

This will only be called by the framework on API 34 and above.

remove

Added in 1.0.0
@MainThread
fun remove(): Unit

Removes this callback from any OnBackPressedDispatcher it is currently added to.

Public properties

isEnabled

Added in 1.0.0
var isEnabledBoolean

The enabled state of the callback. Only when this callback is enabled will it receive callbacks to handleOnBackPressed.

Note that the enabled state is an additional layer on top of the androidx.lifecycle.LifecycleOwner passed to OnBackPressedDispatcher.addCallback which controls when the callback is added and removed to the dispatcher.