Home > Mobile >  How to prevent opening a new fragment by clicking on the one just opened?
How to prevent opening a new fragment by clicking on the one just opened?

Time:06-01

I created the grid of the items using the RecyclerView. When a user clicks the item, the full screen fragment opens. It works well. But I see the weird situation when a user clicks on any full screen fragment surface the new fragment from the RecyclerView grid opens. It turns out that the user, as it were, clicks on the button under the fragment. How can this be avoided?

Here is a chunk of the code how the click listening is organized:

SetGrid.adapter = TestAdapter{ position ->

        when (position) {

            0 -> {
                val transaction = supportFragmentManager.beginTransaction()
                transaction.replace(R.id.fr_frame, OneFragment())
                transaction.disallowAddToBackStack()
                transaction.commit()
            }

            1 -> {
                val transaction = supportFragmentManager.beginTransaction()
                transaction.replace(R.id.fr_frame, TwoFragment())
                transaction.disallowAddToBackStack()
                transaction.commit()
            }

           2 -> {
                val transaction = supportFragmentManager.beginTransaction()
                transaction.replace(R.id.fr_frame, ThreerFragment())
                transaction.disallowAddToBackStack()
                transaction.commit()
            }

      }

CodePudding user response:

From what you said your fragment in the background is triggering the on-touch navigations. You can remove it from the back stack as I see you used disallowAddToBackStack(). I suspect it is not working as intended so its better to "popBackStack() " in the oncreate method of the fragment.

  • Related