简体   繁体   English

每个底部导航视图菜单项的单独导航图 - Android

[英]Individual Navigation graph for each Bottom navigation view menu item - Android

My application contains a bottom navigation view with 3 menu items in the main activity, each menu item inflates respective navigation graphs in the navigation container view.我的应用程序在主活动中包含一个底部导航视图,其中包含 3 个菜单项,每个菜单项都会在导航容器视图中扩展各自的导航图。 Each graph has 2 or more fragments connected via actions.每个图都有 2 个或更多通过动作连接的片段。

The problem here is during screen orientation change the application gets crashed.这里的问题是在屏幕方向更改期间应用程序崩溃了。 Also, bottom navigation does not preserve navigation graphs's state and there is no backstack maintained for the bottom navigation.此外,底部导航不保留导航图的 state 并且没有为底部导航维护回栈。

Code sample below.下面的代码示例。

val bottomNavigation = binding.bottom_navigation_view
navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController

bottomNavigation.setOnNavigationItemSelectedListener{ item ->
            when(item.itemId){
                R.id.navigation_home ->{
                    val navGraph = navController.navInflater.inflate(R.navigation.nav_graph)
                    navController.graph = navGraph
                    true
                }
                R.id.navigation_search ->{
                    val searchGraph=navController.navInflater.inflate(R.navigation.search_nav_graph)
                    navController.graph = searchGraph
                    true
                }
                R.id.navigation_about ->{
                    val infoGraph = navController.navInflater.inflate(R.navigation.info_nav_graph)
                    navController.graph = infoGraph
                    true
                }
            }
            false
        }

Create NavigationExtension class创建导航扩展 class

take FragmentContainerView to load fragments使用 FragmentContainerView 加载 Fragment

 <androidx.fragment.app.FragmentContainerView
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true" />

Setup NavigationGraph in the Activity在 Activity 中设置 NavigationGraph

private fun setupBottomNavigationBar() {
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)

        val navGraphIds = listOf(R.navigation.nav_graph, R.navigation.search_nav_graph, R.navigation.info_nav_graph)

        // Setup the bottom navigation view with a list of navigation graphs
        val controller = bottomNavigation.setupWithNavController(
            navGraphIds = navGraphIds,
            fragmentManager = supportFragmentManager,
            containerId = R.id.nav_host_container,
            intent = intent
        )

        // Whenever the selected controller changes, setup the action bar.
        controller.observe(this, Observer { navController ->
            setupActionBarWithNavController(navController)
        })
        currentNavController = controller
    }

Note: Bottom navigation item id and navigation graph id should be the same注意:底部导航项 id 和导航图 id 应该相同

for more detail you can refer to this sample project有关更多详细信息,您可以参考此示例项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM