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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With