androidnavigationuinavigationcontrollerandroid-preferences

SettingsFragment with navhost using bottom navigation selector


I have a standard bottom navigation selector using a navhost controller. I would like to add a PrefencesFragment that will be full screen. My issue is if I use the navcontroller it will use the navhost fragment which has the bottom navigation below it which will look weird.

I know I have to use a global destination as per documentation --> https://developer.android.com/guide/navigation/navigation-global-action

But how do I get it to replace the whole layout and not just navhost fragment?

I almost wish that I could just launch an activity and put the fragment in there. Would that be very bad style?

Here is my layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
        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">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:logo="@drawable/baseline_post_add_24"
                app:logoDescription="Postering With Passion"
                app:title="Postering With Passion"
                app:titleCentered="true"
                app:titleTextColor="@color/white"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="0dp" >

            <ProgressBar
                android:id="@+id/toolbar_progress_bar"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:indeterminateTint="#cccccc"
                android:indeterminateTintMode="src_in"
                android:indeterminate="true"
                android:layout_marginRight="4dp"
                android:layout_gravity="right"
                android:visibility="visible"
                />

            </com.google.android.material.appbar.MaterialToolbar>

        </com.google.android.material.appbar.AppBarLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_view"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_height="0dp"
        tools:context="com.plcoding.posterpalfeature.ui.activity.MainActivity">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <fragment
                android:id="@+id/navHostFragment"
                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/job_nav_graph" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <include
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/background_bottom_sheet"
            app:layout_anchorGravity="bottom"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            layout="@layout/layout_bottom_sheet"></include>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/bottom_navigation_menu"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:labelVisibilityMode="labeled"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"/>

        </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Here is my menu XML. I haven't added anything yet

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:id="@+id/jobsListFragment"
        android:title="All Jobs List"
        android:icon="@drawable/baseline_add_box_24"/>
    <item  android:id="@+id/jobDetailFragment"
        android:title="Job Details"
        android:icon="@drawable/baseline_assignment_24"/>
    <item  android:id="@+id/mapFragment"
        android:title="Postering Map"
        android:icon="@drawable/baseline_map_24"/>
</menu>

Here is the navgraph

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/job_nav_graph"
    app:startDestination="@id/jobsListFragment">

        <fragment
            android:id="@+id/jobsListFragment"
            android:name="com.plcoding.posterpalfeature.ui.fragments.JobsListFragment"
            android:label="JobsListFragment"
            tools:layout="@layout/fragment_job_list">
            <action
                android:id="@+id/action_jobsListFragment_to_mapFragment"
                app:destination="@id/mapFragment"
                app:enterAnim="@anim/slide_in_left"
                app:exitAnim="@anim/slide_out_right"
                app:popEnterAnim="@anim/slide_in_left" />
            <action
                android:id="@+id/action_jobsListFragment_to_jobDetailFragment"
                app:destination="@id/jobDetailFragment"
                app:popUpTo="@id/jobsListFragment"
                app:popUpToInclusive="true" />
            <action
                android:id="@+id/action_jobsListFragment_to_settings_graph"
                app:destination="@id/settings_graph" />
        </fragment>
        <fragment
            android:id="@+id/jobDetailFragment"
            android:name="com.plcoding.posterpalfeature.ui.fragments.JobDetailFragment"
            android:label="JobDetailFragment"
            tools:layout="@layout/fragment_job_detail">
            <argument
                android:name="jobId"
                android:defaultValue="1"
                app:argType="integer" />
        </fragment>
        <fragment
            android:id="@+id/mapFragment"
            android:name="com.plcoding.posterpalfeature.ui.fragments.MapFragment"
            android:label="MapFragment"
            tools:layout="@layout/fragment_map_detail" />

        <include app:graph="@navigation/settings_graph"/>

</navigation>

And the settings graph

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:id="@+id/settings_graph"
    app:startDestination="@id/settingsFragment">

    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.plcoding.posterpalfeature.ui.activity.SettingsFragment"
        android:label="SettingsFragment" />
</navigation>

I am able to navigate to a dummy fragment and hide the bottom view but I don't see and back or home button when I'm in the settings fragment. I also get a crash when I click on the settings icon when in that fragment.

Finally, when I use

setupActionBarWithNavController(navController, AppBarConfiguration(navController.graph))

I see the back button but i can't control the color of the arrow and clicking on it does nothing

I can always create a new menu but it feels like I'm doing something wrong

Thank you for taking the time to read my question!


Solution

  • To hide navigation view likes (BottomNavigation, Toolbar, etc), add a OnDestinationChangedListener, You will get a call when the navigation host changes its fragment. Then you can show or hide the navigation views based on which fragment is showing.

    One more thing, if your bottom_sheet is not always visible, you do not need to define it in your layout. It can be shown from NavController as a BottomsheetDialog with a dialog tag.