14

I'm trying to achieve a simple design. One activity with a host fragment.

The thing is, one of the destinations has a bottom navigation bar.

Here's a paint sketch enter image description here

After a little bit of research, I discovered that the best practice is to have a single Activity with a host fragment.

In my particular case, the bottom navigation bar should not be visible in the login and register fragments and just hiding it doesn't seem right to me.

I managed to create an activity with a bottom navigation bar connecting main fragment to frag 1, frag 2 or frag 3, but now I need to add the login fragment and register fragment and I'm not sure how to handle the navigation.

Here's the code for the app without the authentication fragments.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context=".Presentation.MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="Glucose Entries"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?colorPrimary"
        android:theme="@style/ToolbarTheme"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_navigation_bar"/>

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

</LinearLayout>

Also:

class MainActivity : AppCompatActivity() {

    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        navController = Navigation.findNavController(this, R.id.nav_host_fragment)
        bottom_nav.setupWithNavController(navController)
        NavigationUI.setupActionBarWithNavController(this, navController)
    }

    // stuff
}

And the navigation file:

<?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/mobile_navigation"
    app:startDestination="@id/mainListFragment">

    <!--frag 1 -->
    <fragment
        android:id="@+id/mainListFragment"
        android:name="com.gluco.Presentation.MainList.MainListFragment"
        android:label="Glucose Entries"
        tools:layout="@layout/main_list_fragment">
    </fragment>

    <!--frag 2 -->
    <fragment
        android:id="@+id/statisticsFragment"
        android:name="com.gluco.Presentation.Statistics.StatisticsFragment"
        android:label="statistics_fragment"
        tools:layout="@layout/statistics_fragment" />

    <!--frag 3 -->
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.gluco.Presentation.Settings.SettingsFragment"
        android:label="SettingsFragment" />

</navigation>

Should I just hide the navigation bar on fragments where is not supposed to be?

Also, I'm thinking another solution could be to not use the automatic navigation provided from the bottom navigation bar and create my own actions to navigate from main fragment to frag 1, frag 2 or frag 3

  • 1
    Can you put your solution? Even I am stuck on the same issue. – sagar suri Jun 15 '19 at 07:19
  • 1
    What I ended up doing was using a single nav host fragment in the main activity with a bottom navigation bar. Then I would hide the bar based on the fragment I was in. To decide where to navigate when the app is launched, I used a simple if statement. Check this: [this](https://stackoverflow.com/questions/51929290/is-it-possible-to-set-startdestination-conditionally-using-android-navigation-ar), it's somehow related. Here's a [link](https://github.com/GhimpuLucianEduard/Mobile/tree/master/App) to the project, it's a school project so you might find other small issues – Ghimpu Lucian Eduard Jun 15 '19 at 16:47
  • When you have such cases, where you have NavHost inside another, like bottom navigation, and on click of any item inside bottomnvaigation item, show a new screen without bottom nav, create new graph with top level destinations, and use activity with nav controller to set the destinations. if it is not clear, let me know, i will share a detailed answer as comment. – Insane Developer Jul 11 '19 at 11:42
  • TS asked really good qquestion. Even now in 2022 I am not able to implement this graps. Navigation does not save state of fragments when we show bottom view – Vetalll Jun 30 '22 at 12:06

1 Answers1

1

Imho, it sounds fine to me to have your single activity be the source of truth as to which fragment shows and hides the navigation bar.

Aaron Dishman
  • 111
  • 1
  • 6
  • In addition to this, I would like to link [this](https://stackoverflow.com/questions/51929290/is-it-possible-to-set-startdestination-conditionally-using-android-navigation-ar), In particular, Danish Khan's answer – Ghimpu Lucian Eduard Jan 03 '19 at 11:01