Search code examples
androidandroid-jetpackandroid-jetpack-navigation

How to use jetpack navigation where project starts with simple fragments and navigate to page with bottomnavigation?


As I understand single activity should have it’s own single navigationHostFragment. The samples I have checked for example if sample has BottomNavigationView the activity’s xml looks like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/nav_host_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/color_black"
        android:layout_weight="1" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemIconTint="@color/color_white"
        app:itemTextColor="@color/color_white"
        app:menu="@menu/bottom_nav"/>
</LinearLayout>

//and the samples where it is simple fragments naivgation it looks simple

<FrameLayout
    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"
    android:background="@android:color/background_light"
    tools:context="com.example.android.navigationsample.MainActivity">

    <fragment
            android:id="@+id/my_nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/navigation"/>
</FrameLayout>

Now I am fondering If I have a project which starts with SetupFragment then should navigation to LoginFragment if user is not loggedin or to homeFragment which has the BottomNavigationView. I do not know how my Activity xml should look like? I can create the navigation graph but I don't know how my activity's xml should look like?


Solution

  • I found the solution watching these videos Highly recommend to watch this if you need to implement advance navigation https://www.pluralsight.com/courses/android-navigation-architecture-components-getting-started