Implementing Bottom navigation on android can be a tricky task, since there isn’t clear instructions on how it can be implemented properly, in this article we are going to implement the material design ‘bottom navigation view’ with multiple back stacks that preserve their state when switching between different screens.
Setup
To use bottom navigation view we will need the material dependency get the latest version here.
In our project we want to display a bottom navigation bar with three options Home, Profile and Favorites so lets create these fragments by right clicking our app package name and select new > Fragment > Blank
These fragments might have a lot redundant code that we won’t need so lets delete it and replace each fragment with this (don’t forget to change names for each fragment)
To display our options in a bottom navigation view we will have to create a menu with the appropriate title, icon and id for each item. to create a menu right click on the res folder in android studio > New > Android Resource Directory, then from the resource type drop down choose menu and click OK.
Right click your newly created menu directory > New > Menu Resource File.
Add 3 items to your new menu with ids and icons, Notice the name of the ids for the menu items as they are important for navigation later.
Right click the res folder > New > Android Resource Directory, from the window choose navigation as the resource type. Now create 4 new navigation graphs in this resource directory, the first one will be called main_graph the rest 3 will have the exact name as the ids from the menu items we just created (home_nav, profile_nav, favorite_nav)
Open up home_nav click the New Destination button and add your home fragment
Repeat the same for profile_nav and favorite_nav, and add the appropriate fragment for each one of them.
On main_graph click the New Destination button then add home_nav.xml, profile.xml and favorite.xml
Feel free to add more fragments and connect them with actions in each of your navigation graphs.
Connecting everything with bottom navigation view
In our activity_main.xml we want to add we will add a bottom navigation view and fragment container. Notice the attributes on fragment container name and navGraph.
navGraph points to the main graph that includes all of our other graph files, name value is set to NavHostFragment which is a class in androidx libraries that provides an area within your layout for self-contained navigation to occur. the NavHostFragment has a navController and a navigation state meaning the state of each of our options in the bottom navigation view will be saved and restored when switching items.
defaultNavHost gives us the expected behavior to navigate to the previous fragment when we press the back button.
And last but not least we’ll have to attach the navController to our bottom navigation view in MainActivity
ActivityMain.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.bottomnavsample.databinding.ActivityMainBinding
class MainActivity :AppCompatActivity(){privatelateinitvar binding: ActivityMainBinding
privatelateinitvar navController: NavController
privatelateinitvar appBarConfiguration: AppBarConfiguration
overridefunonCreate(savedInstanceState: Bundle?){super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)setContentView(binding.root)val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)as NavHostFragment
navController = navHostFragment.findNavController()//Give the action bar the correct title for the current screen (this uses the fragment label)
appBarConfiguration =AppBarConfiguration(setOf(R.id.homeFragment, R.id.profileFragment, R.id.favoriteFragment2))//Connects the action bar with the nav controllersetupActionBarWithNavController(navController, appBarConfiguration)
binding.bottomNav.setupWithNavController(navController)}//gives functionality to the back button on the action baroverridefunonSupportNavigateUp(): Boolean {return navController.navigateUp(appBarConfiguration)}}