Navigation Architecture Component - BottomNavigationView with Login Screen

徘徊边缘 提交于 2020-05-16 05:56:11

问题


I have my MainActivity setup which looks like this :

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.host.HostActivity">



    <fragment
        android:id="@+id/mainNavigationFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/main_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

And the bottom navigation has 3 tabs which can only be shown after some initial authentication and setup user calls have been performed.

What is the best way to show a Loading/Setting up user screen before I let user start using the bottom nav view.

One way I am thinking is to delay the setup of NaHostFragment in my Activity until all the setup is done. So basically toggle the visibility of mainNavigationFragment and BottomNavigationView till the setup is completed.

Are there any other ideas ?


回答1:


The NavigationUI documentation actually uses hiding a BottomNavigationView as its example. You could also use an OnDestinationChangedListener to update the visibility of your BottomNavigationView, say, only hiding it while the user is on the login screen (note that as per the Conditional Navigation documentation, you shouldn't be doing this in the start destination of your graph, but redirecting users to those screens):

navController.addOnDestinationChangedListener { _, destination, _ ->
  if(destination.parent?.id == R.id.login) {
    bottomNavigationView.visibility = View.GONE
  } else {
    bottomNavigationView.visibility = View.VISIBLE
  }

}



来源:https://stackoverflow.com/questions/61604091/navigating-between-fragment-bug-bottomnavigationview-visibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!