Navigation Component - All the best practices
Navigation Component Implementations
Conditional Navigation
- When we conditionally navigate to different fragments based upon a certain conditions.
- Example. Navigate to HomeFragment when user is logged in otherwise navigate to LoginFragment.
Back Stack Manipulation
- Navigation Controller handles the back stack for us. What this means is when we navigate to a destination, it's automatically adding each destination to the back stack.
- The navigation action can manipulate the back stack as it moves to the next destination.
- Sometimes we need actions pop destinations off of the back stack according to our logical conditions for this we use the pop behaviour attributes from the editor.
- PopUpTo : desired fragment and select inclusive true, It tells the navigation component to pop FragmentTransaction over the fragment back stack until it finds the one that returns the desired fragment.
- apppopUpToInclusive="true" will clear the back stack and let you exit.
Adding Up Button in Action Bar using Navigation Controller
- The Navigation Controller has a UI library called navigation UI
- It integrates with the Action Bar to implement the correct behaviour for the Up button.
- To hook up the button to navigation, navigation UI needs access to the navigation controller.
- The preferred way of finding the Navigation Controller in an activity is to use the findNavController method that takes the activity in the NavHostFragment parameters.
Use case :
- First find the navController from your navigation host fragment
- Link the navController to our Action Bar
- use Ctrl+ O in Android Studio to override any methods
- override onSupportNavigateUp(), here we find the navController and call navigateUp
// find the navController from your navigation host fragment
val navController = this.findNavController(R.id.navigationHost)
// link the navController to our action bar
NavigationUI.setupActionBarWithNavController(this, navController)
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.navigationHost)
return navController.navigateUp()
}
Adding Menu
Menus are created from menu XML resources.
Use Case:
- Create a new menu resource file
- Add menu item
- Call setHasOptionsMenu(true) : this tells Android that our fragment has an Options Menu
- Note: if you add menu to the container activity, it is displayed every fragment that it contains, so if you set the menu to fragment it is displayed to particular fragment only.
- override onCreateOptionsMenu and inflate menu resource
- override onOptionsItemSelected and call onOptionsItemSelected to connect to our NavigationUI
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater?.inflate(R.menu.overflow_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return NavigationUI.onNavDestinationSelected(item!!,
view!!.findNavController())
||super.onOptionsItemSelected(item)
}
Comments
Post a Comment