Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 604
Johann Avatar asked May 17 '19 10:05

Johann


People also ask

Can we use navigation component for activities?

To take full advantage of the Navigation component, your app should use multiple fragments in a single activity. However, activities can still benefit from the Navigation component. Note, however, that your app's UI must be visually broken up across several navigation graphs.

Can we add activity in navigation graph?

Add destinations to the navigation graphYou can create a destination from an existing fragment or activity. You can also use the Navigation Editor to create a new destination or create a placeholder to later replace with a fragment or activity.

How can I call a navigation drawer from another activity?

Basically Each Activity has its own actionBar and each actionbar can use own Navigation Drawer. So If You want to same navigation Drawer. You can use Fragment. You can make MainActivity has Navigation Drawer and Use FrameLayout in Mainactivity Then Replace Fragment.

What are the components of navigation?

The Navigation component contains a default NavHost implementation, NavHostFragment , that displays fragment destinations. NavController : An object that manages app navigation within a NavHost . The NavController orchestrates the swapping of destination content in the NavHost as users move throughout your app.


2 Answers

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

like image 180
Srikar Reddy Avatar answered Oct 21 '22 02:10

Srikar Reddy


Answer is Unnecessary.

In Navigation Component idea, you need to have 1 + 3 parts and unlimited fragments.

You can watch Google Navigation Component Video.

Only one Activity.

  1. Single Activity

These are working in the one Activity (Single Activity).

  1. Navigation graph
  2. NavHostFragment
  3. 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

like image 24
DevPolarBear Avatar answered Oct 21 '22 02:10

DevPolarBear