Ad

Using The Navigation Component With Multiple Activities

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?

Ad

Answer

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-migratehttps://developer.android.com/guide/navigation/navigation-ui

Ad
source: stackoverflow.com
Ad