I am looking for example with Navigation Component in Android that will use multiple navigation graphs and then show how to nest them together?
Could you give any example or hint? It is new feature in Android, and there are only simple Single Activity examples with NavHostFragment, but how to use multiple navigation graphs correctly nest them or switch from one nav graph to another. What are the best practices?
Related
What's the best way to implement navigation using single activity architecture and Jetpack navigation for an application with complex navigation that involves some different navigation strategies. For example:
Main application navigation using bottom navigation
In some of the bottom tabs fragments you can access some detail action that has its own navigation. For example, a detail view implemented:
as a full-screen fragment
without the bottom bar
with a top header with two tabs.
What's the best way to combine the main navigation with the two tabs from the detail view, or in general, more nested strategies. It's better to use several navigation files or can be implemented using only one?
I would like to know what is the best practice of using Navigation Component + BottomNavigationView + NavigationDrawer.
I have tried google's advanced sample for Navigation component. It worked very well for multiple back stack modules. Because it has a workaround extension. When using this approach, each bottom tab has its own graph and the graphs change as you select one of the tabs.
But when NavigationView is integrated, it needs to know the navigation graph in advance. But in advanced sample the navGraph attribute is not used, graphs added programmatically for bottomNavigation.
So how do we manage graphs when both Bottom Navigation View and Navigation Drawer View are used together?
After experimenting with different approaches, I have come to the conclusion, when using both BottomNavigationView and NavigationDrawer, It's best practice to keep your navigation graph single. When having multiple navigation graphs, it will be hard to manage logical connections between screen.
To have a control over the toolbar and bottomNavigationView's visibility, I use addOnDestinationChangedListener
navController.addOnDestinationChangedListener { controller, destination, arguments ->
when(destination.id){ //check with destination ids, }
it may not be best practice. If you have any better ways, please share it
In the Android docs, it states:
The Navigation component is designed for apps that have one main activity with multiple fragment destinations. The main activity is associated with a navigation graph and contains a NavHostFragment that is responsible for swapping destinations as needed. In an app with multiple activity destinations, each activity has its own navigation graph.
Does this mean that you cannot use the Navigation component to navigate from one activity to another? That appears to be the case.
Second question: If I create an app that uses the navigation drawer, the default code that created when you add an activity that is to have a navigation drawer already has code for managing navigation from one drawer item to another. So is the Navigation component also kind of useless here?
Does Google want us to be creating only single activity apps?
Does Google want us to be creating only single activity apps?
Single activity architecture is something you can move towards. It is not restricted (just recommended) by Google. The architecture has its own advantages and drawbacks. You don’t have to tear up your entire app simply to add Navigation Component. Evaluate and Decide whether it’s worth the pain.
Does this mean that you cannot use the Navigation component to
navigate from one activity to another
No, You can use Navigation Component to replace startActivity calls. Simply add your Second Activity Nav Graph to the First Activity Nav Graph and use the nav controller to navigate between the two.
findNavController().navigate(directions)
Here is a migration guide for it https://developer.android.com/guide/navigation/navigation-migrate#add
In cases, where you want to use a different activity, you can evaluate whether you need a different activity or a different task.
If I create an app that uses the navigation drawer, the default code
that created when you add an activity that is to have a navigation
drawer already has code for managing navigation from one drawer item
to another. So is the Navigation component also kind of useless here?
or
instead of using the default code for a navigation drawer to build
your own navigation drawer that is more inline with the Navigation
component
The thing is you don’t have to build a custom component or anything complicated. Actually, use of the Navigation Component (with the help of NavigationUI class) simplifies the code for the drawer layout and its listeners.
At this link, the documentation helps you implement the Navigation component when using Navigation Drawer and Bottom Navigation View.
With regard to the generated templates, those are outdated and need an upgrade.
References:
https://developer.android.com/guide/navigation/navigation-migrate
https://developer.android.com/guide/navigation/navigation-ui
Short Answer is Unnecessary because:
In Navigation Component idea, you need to have 1 + 3 parts and unlimited fragments.
You can watch Google Navigation Component Video.
Only one Activity.
Single Activity
These are working in the one Activity (Single Activity).
Navigation graph
NavHostFragment
NavController
Why Unnecessary? Because, all parts of "1 + 3" connected with each other.
Details:
Navigation graph is connected with NavFostFragment. Moreover, NavFostFragment defines in XML file of Single Activity. Also, NavController defines by NavController as "navHostFragment.navController".
However, if you really really want to use Navigation Compenent for Activities, you need to use add fragments in Activities.
For Example:
[Activity_A + Fragment_A] and [Activity_B + Fragment_B]
The idea of solution is:
For Activity_A to Activity_B: Navigate Fragment_A -> Activity_B
OR
You can migrate. For Activity_A to Activity_B: Navigate Fragment_A -> Activity_B
More detail: Migrate to the Navigation component by Google
Is it possible to share navigation graph between two (or more) containers?
What I would like to achieve is to have two containers (NavHosts):
one smaller that is above bottom navigation and below toolbar
second is full screen.
I would like to have one navigation graph, because fragments from one container can trigger actions from other one. Otherwise it (having more navigation graphs that interacts with each other) will become untidy and problems created by library will surpass advantages.
Seems like you cannot, however you can have nested navigation: i.e. wrap your toolbar navigation inside of fullscreen (of vice versa) and playing with
navController.popBackStack(<id of nested item>)
Yes, it is possible. Take a look at this sample. This is an implementation of Android Navigation component with bottom navigation view with separate history for each tab. You can use the concept and change it the way that fits your app
I was reading about Navigation Drawer and it was not until now that I came into this question: If my top views are going to be accessible through the Navigation Drawer, should I code each of them as Fragments or as Activities?
I want to know the way it was intended to be used, with Fragments or with Activities
The best way is to use Fragments.
When you click on an item, you should always replace the Fragment of your Main Activity without adding the elements in the Back Stack. This will create a seamless dynamic navigation instead of launching new activities and managing the drawer into them.
Here are some references
Google's design guidelines
Google's development guidelines