Ale*_*289 5 navigation android android-architecture-components android-jetpack android-architecture-navigation
so I am trying to learn about using Navigation graph in Android, I am not sure what I am doing, so maybe you have better way.
let say there are 2 sections in my app:
Section one is called Main section, which has bottom navigation view and toolbar
Section two is called Authentication Section (for login page, sign up) , it doesn't has bottom navigation view or toolbar
so because these 2 sections has different 'frame' (one has bottom navigation and toolbar, the other doesn't) so I make 2 activities. MainActivity and AuthenticationActivity that will host the fragment
here is the xml of MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_graph"
app:defaultNavHost="true"
/>
<android.support.design.widget.BottomNavigationView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorPrimary"
app:itemIconTint="@color/color_bottom_view_navigation"
app:itemTextColor="@color/color_bottom_view_navigation"
app:menu="@menu/menu_bottom_view"
app:labelVisibilityMode="labeled"
android:id="@+id/bottom_nav"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
and here is the xml of AuthenticationActivity:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.LoginActivity"
android:background="@android:color/white">
<fragment
android:id="@+id/nav_login_host_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_main"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
so in the HomeFragment class, I want to make a logic. if the user has logged in before then stay in the MainActivity (in home fragment), otherwise I want my app to move to AuthenticationActivity to show the LoginFragment
but how to do that ?
I have no idea how to set that in the navigation graph xml (how to set the destination and action), and I also have no idea how to set up the code in the kotlin file to move to loginFragment from HomeFragment
I previously thought that in the main navigation graph I make the nested graph like this
and when I click that nested graph it seems I can't choose the host.
in the HomeFragment kotlin file I am confused how to make action to that authenticationActivity (to LoginFragment), I need to make logic like this:
if (userHasLoggedIn) {
Navigation.findNavController(it).navigate(loginDestination)
}
Run Code Online (Sandbox Code Playgroud)
但是主机仍在使用MainActivity,这意味着它具有底视图和工具栏。在“登录”中,我不需要底部导航视图和工具栏。
老实说,我不确定是否可以使用2种活动将其分开,也许您有更好的方法?