private val onBackPressedCallback: OnBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// Do your stuff
}
}
if (Build.VERSION.SDK_INT >= 33) {
onBackInvokedDispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT
) {
// Do your stuff
}
} else {
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
}
Adapt to
onBackPressedevent to work on both the latest and old versions of Android. Since API 33, OnBackInvokedDispatcher is the ideal way to handle that.Ref : https://developer.android.com/reference/android/window/OnBackInvokedDispatcher
Ideal Approach: