Android Fragments 如何防止在BottomNavigation的Main fragmet中重新打开碎片?

5jvtdoz2  于 2023-08-06  发布在  Android
关注(0)|答案(1)|浏览(120)

我有5个itemBottomNavigationBarMainActivity类中。当打开EarningFragment时,它有FragmentContairView,它从EarningFragment打开MoneyDetailFragment。当我打开这个MoneyDetailFragment时,它打开了。
在这个场景中,当单击BottomNavigationBar上的另一个项目,然后再次单击EarningFragment的项目时,它会记住MoneyDetailFragment中的项目,并打开这个类。我不想让它记起来在哪。我希望它只打开EarningFragment时,点击它在BottomNavigationBar。太复杂了,希望你能理解。
MainActivity.kt

class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedListener,
                     View.OnClickListener {
private lateinit var navHostFragment: NavHostFragment

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
 navHostFragment.navController.addOnDestinationChangedListener(this)
}
     private fun setupBottomNavigationView() {
     BottomNavigationViewHelper.setupBottomNavigationView(binding.bottomBar.bottomNavViewBar)
     binding.bottomBar.bottomNavViewBar.setupWithNavController(navHostFragment.navController)
        }
}

字符串
nav_graph_main.xml

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/nav_graph_main"
        app:startDestination="@id/miHome">
    
        <fragment
            android:id="@+id/miHome"
            android:name="com.mi.ui.home.HomeFragment"
            android:label="@string/menu_home"
            tools:layout="@layout/fragment_home">
            <action
                android:id="@+id/action_to_earningFragment"
                app:destination="@id/miEarnings" />
            <action
                android:id="@+id/action_to_moneyGoldRegisterFragment"
                app:destination="@id/moneyGoldRegisterFragment" />
</fragment>
    <fragment
        android:id="@+id/miEarnings"
        android:name="com.mi.ui.earning.EarningFragment"
        android:label="@string/menu_bottom_earnings"
        tools:layout="@layout/fragment_earning">
        <action
            android:id="@+id/action_to_moneyCardFragment"
            app:destination="@id/moneyCardFragment" />
        <action
            android:id="@+id/action_to_moneyDetailFragment"
            app:destination="@id/moneyDetailFragment" />
    </fragment>

yyyllmsg

yyyllmsg1#

@DavidWasser非常感谢你。用户为“饼干”命名的第二个链接,我从它解决。我将这些代码添加到MainActivity中,以清除要打开的导航状态。

binding.bottomBar.bottomNavViewBar.setOnItemSelectedListener {
            when (it.itemId) {
                R.id.miHome -> {
                    navHostFragment.navController.navigate(R.id.miHome)
                    true
                }
                R.id.miBarcode -> {
                    navHostFragment.navController.navigate(
                        R.id.miBarcode,
                        null,
                        NavOptions.Builder()
                            .setPopUpTo(R.id.miBarcode, true)
                            .build())
                    true
                }
                R.id.miEarnings -> {
                    navHostFragment.navController.navigate(
                        R.id.miEarnings,
                        null,
                        NavOptions.Builder()
                            .setPopUpTo(R.id.miEarnings, true)
                            .build())
                    true
                }
                R.id.miProfil -> {
                    navHostFragment.navController.navigate(
                        R.id.miProfil,
                        null,
                        NavOptions.Builder()
                            .setPopUpTo(R.id.miProfil, true)
                            .build())
                    true
                }
                else -> {
                    navHostFragment.navController.navigate(R.id.miHome)
                    true
                }
            }
        }

字符串

相关问题