android-gestureandroid-13gesture-navigation

OnBackInvokedCallback is not enabled for the application in Set 'android:enableOnBackInvokedCallback="true"' in the application manifest as warning


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.

Solution

  • 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.