1

I'm trying to test 'predictive back gesture' feature, implemented in Android 13. In order to do that, first I've migrated my app to AndroidX API (so got rid of all the usages of onBackPressed methods), then enabled this feature in Manifest and tried on Pixel 4a with Android 13 installed - it didn't work.

Did some investigation and then finally made it to work by removing all the onBackPressed callbacks within onBackPressedDispatcher.

My question is - how to make this feature to work, while having registered onBackPressed callbacks in the app?

Update:

abstract class BaseFragment : Fragment() {

    private val onBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            onBackPressed()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, onBackPressedCallback)
    }

// If there are no more items in backstack, `navController.navigateUp()` 
// closes the app, which is desired behavior. It's triggered via callback though, 
// so, as I understand, that's the reason predictive back gesture doesn't work. 
    protected open fun onBackPressed() {
        if (!navigateUp()) {
            onBackPressedCallback.isEnabled = false
            requireActivity().onBackPressedDispatcher.onBackPressed()
        }
    }

// I can override this method in places, where I need to use custom backPressed logic
    protected open fun navigateUp(): Boolean = navController.navigateUp()

}
Myroslav
  • 896
  • 12
  • 21
  • If you have a callback enabled, then yeah, your callback is going to be called. If you want to have your activity close when the system back gesture is triggered, why do you have an enabled callback? Show your code. – ianhanniballake Sep 06 '22 at 18:35
  • default implementation of app's backPressed callback is `navController.navigateUp()`. This closes the app in case there are no more items in backstack. I would like to keep this logic (allows to ovveride default implementation in places, where I need this), but still be able to use predictive back gesture. Looks like with its current implementation it's not possible. – Myroslav Sep 07 '22 at 19:38
  • You don't need any of that code at all. Navigation already intercepts and does the right thing with the back button for you. What makes you think you need any of that code? – ianhanniballake Sep 07 '22 at 22:08
  • @ianhanniballake App is using single activity pattern, so there are parent fragments, which may include several child fragments. That code helps to manage both in-app and 'physical' back navigation and keep them consistent. But thanks for suggestions, maybe it should be reviewed, so that indeed I don't have to call `navController.navigateUp()` manually. – Myroslav Sep 12 '22 at 07:38
  • Fragments automatically handle dispatching the back button to the child FragmentManager as well if it has a back stack. – ianhanniballake Sep 12 '22 at 13:58
  • in case of when we have overriden backpress dispatcher , added code to execute and call navController. popUpStackBack() will this ended up breaking predictive back preview when it will be expanded to individual screens apart from current launcher animation ? is it best to override in case where we really needed something like confirmation dialog or something – ud_an Sep 12 '22 at 23:57

0 Answers0