28

I'm running my Android Application `Android-13, in the Logcat I'm seeing this warning, How to resolve this?

OnBackInvokedCallback is not enabled for the application.
Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
FGH
  • 2,900
  • 6
  • 26
  • 59

1 Answers1

27

This is because of the Android Gesture Navigation, reference link here

To help make predictive back gesture helpful and consistent for users, we're moving to an ahead-of-time model for back event handling by adding new APIs and deprecating existing APIs.

The new platform APIs and updates to AndroidX Activity 1.6+ are designed to make your transition from unsupported APIs (KeyEvent#KEYCODE_BACK and OnBackPressed) to the predictive back gesture as smooth as possible.

The new platform APIs include OnBackInvokedCallback and OnBackInvokedDispatcher, which AndroidX Activity 1.6+ supports through the existing OnBackPressedCallback and OnBackPressedDispatcher APIs.

You can start testing this feature in two to four steps, depending on your existing implementation.

  1. Upgrade to AndroidX Activity 1.6.0-alpha05. By upgrading your dependency on AndroidX Activity, APIs that are already using the OnBackPressedDispatcher APIs such as Fragments and the Navigation Component will seamlessly work when you opt-in for the predictive back gesture.
  // In your build.gradle file:
    dependencies {

  // Add this in addition to your other dependencies
  implementation "androidx.activity:activity:1.6.0-alpha05"
  1. Opt-in for the predictive back gesture. Opt-in your app by setting the EnableOnBackInvokedCallback flag to true at the application level in the AndroidManifest.xml.
<application

    ...

    android:enableOnBackInvokedCallback="true"

    ... >

...

</application>

If your app doesn’t intercept the back event, you're done at this step. Note: Opt-in is optional in Android 13, and it will be ignored after this version.

 val onBackPressedCallback = object: OnBackPressedCallback(true) {

   override fun handleOnBackPressed() {

     // Your business logic to handle the back pressed event

   }

 }

 requireActivity().onBackPressedDispatcher

   .addCallback(onBackPressedCallback)
  1. When your app is ready to stop intercepting the system Back event, disable the onBackPressedCallback callback.
onBackPressedCallback.isEnabled = webView.canGoBack()

Note: Your app may require using the platform APIs (OnBackInvokedCallback and OnBackPressedDispatcher) to implement the predictive back gesture. Read our documentation for details.

Prashant
  • 394
  • 1
  • 6
  • 18
FGH
  • 2,900
  • 6
  • 26
  • 59
  • 2
    You say "Your app may require using [both]... Read our documentation for details." Can you please point to that documentation? I haven't been able to find anything that talks about when one is required versus the other versus when both are. Thanks. – Kevin Worth Dec 06 '22 at 19:20
  • 1
    https://developer.android.com/guide/navigation/predictive-back-gesture – Tughi Mar 05 '23 at 07:04
  • The Android 13 version of this is really limiting compared to what came before. I notice that Android 14 is adding `OnBackAnimationCallback` to support the full cycle of start/progress/complete-or-cancel. Is there any reason `android:enableOnBackInvokedCallback` can't use a boolean resource keyed to API version? There's not a lot of documentation and I want to know if this is going to be supported rather than just guessing and hoping. – j__m Apr 16 '23 at 19:29