Android - Disable swipe to previous screen

3k Views Asked by At

How to intercept the swipe to left movement to navigate to previous screen and disable the navigation ?

I have tried to intercept the action and return false but it does not work:

@SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    view.setOnTouchListener{ v, event ->
        Timber.d("~ TouchEvent: ${event.toString()}")
        when(event.action){
            ACTION_DOWN -> {
                false
            }

            ACTION_MOVE -> {
                false
            }
        }
    }
}

Is there a way to disable the swipe to previous screen gesture ?

reference: Detect common gestures

1

There are 1 best solutions below

1
On BEST ANSWER

You can set new callback for on back pressed case. And with new callback, you can disable all the actions and do nothing.

Thus, app won't navigate to previous screen when clicked on back button or when used swipe to left gesture.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    requireActivity().onBackPressedDispatcher.addCallback { }
}