导航架构组件中的条件导航

我在我的应用程序中使用单一活动架构,并在我的一个片段中使用像这样的BottomNavigationView .

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  tools:context=".ui.MainNavigationFragment">


<fragment
    android:id="@+id/main_nav_fragment_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:defaultNavHost="true"
    app:navGraph="@navigation/tabs_nav_graph" />


<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemBackground="@drawable/bg_menu"
    app:itemIconTint="@color/menu_item_color"
    app:itemTextColor="@color/menu_item_color"
    app:elevation="10dp"
    app:menu="@menu/bottom_bar_menu"/>

在代码方面

class MainNavigationFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_main_navigation, container, false)
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val host =  
childFragmentManager.findFragmentById(R.id.main_nav_fragment_container) as NavHostFragment

    host.navController.addOnNavigatedListener { controller, destination ->
        logd("Tab NavGraph:${destination.label}")

    }


    bottomNavigationView.setupWithNavController(navController = 
     host.navController)



}

并且在我的第四个选项卡中我正在启动配置文件Fragment并启动登录片段如果用户尚未登录但如果用户在登录片段后没有登录但如果我再次弹出它会再次启动登录片段因为配置文件中的条件分段 .

回答(0)