【问题标题】:Retain which fragment is active in the BottomNavigationView with navigationComponent使用导航组件保留底部导航视图中哪个片段处于活动状态
【发布时间】:2020-07-08 12:36:10
【问题描述】:

感谢导航架构,我用片段设置了我的导航系统。但是,我的主页片段中有一个按钮允许我导航到另一个片段,但是当我单击导航视图的设置菜单时,例如在主页片段上,显示的片段是基本片段,而不是我之前推出的一款。

例子:

我想知道在这种情况下如何显示玩家片段。


我的代码:

主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }
}

导航图:

<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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="fr.frenchapplab.testbottomnav.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_navigation_home_to_playersFragment"
            app:destination="@id/playersFragment" />
    </fragment>

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="fr.frenchapplab.testbottomnav.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="fr.frenchapplab.testbottomnav.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
    <fragment
        android:id="@+id/playersFragment"
        android:name="fr.frenchapplab.testbottomnav.PlayersFragment"
        android:label="fragment_players"
        tools:layout="@layout/fragment_players" />
</navigation>

我使用这段代码来启动播放器片段:

Button mybutton = root.findViewById(R.id.mybutton);
mybutton.setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(R.id.action_navigation_home_to_playersFragment));

【问题讨论】:

    标签: java android navigation uinavigationcontroller bottomnavigationview


    【解决方案1】:

    您可以覆盖 navController 的 addOnDestinationChangedListener() 方法来手动跟踪正在显示的片段,然后创建一些 if 条件以在必要时导航到其他地方。它不是那么优雅,但如果调整正确,这样的东西应该可以工作:

    //declare lateinit var homeFragmentDestingation
     
    navController.addOnDestinationChangedListener(object : NavController.OnDestinationChangedListener {
                override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {
                    
                    //keep track of destinations
                    previousDestination = currentDestination
                    currentDestination = destination
    
                   if (destination.label == "Home" && homeFragmentDestingation?.label == "FILL THIS IN" && maybe other conditions here) {
                        navigateToFragment()
                    } else if (tweak this as necessary) {
                        homeFragmentDestingation = destination
                    }
    
                    Log.d(TAG, "destination is ${homeFragmentDestingation?.label} \n previous destination is ${previousDestination?.label}")
                }
            })
    

    【讨论】:

    • 这是个好主意,但可能会产生一些参数问题..
    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多