android-fragments 如何防止通过点击刚打开的片段来打开新的片段?

pexxcrt2  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(163)

我使用RecyclerView创建了项目网格。当用户点击项目时,全屏片段打开。它工作得很好。但是我看到了一个奇怪的情况,当用户点击任何全屏片段表面时,来自RecyclerView网格的新片段打开。原来,用户点击了片段下面的按钮。如何避免这种情况?
下面是如何组织点击侦听的一段代码:

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()
            }

      }
sg24os4d

sg24os4d1#

根据你所说的,你在后台的片段触发了on-touch导航。你可以从后台堆栈中删除它,因为我看到你使用了disallowAddToBackStack()。我怀疑它没有按预期工作,所以最好在片段的oncreate方法中使用“popBackStack()“。

相关问题