22

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?

RKRK
  • 1,284
  • 5
  • 14
  • 18
Johann
  • 27,536
  • 39
  • 165
  • 279
  • Activities already have its own life-cycle and states. What you want include or join each with Navigation Components? Its a useless thing! – Nick Bapu May 17 '19 at 10:18
  • Navigation componet's use is basically to reduce the call of new activity and for using fragments its somewhat light-weighted. – Nick Bapu May 17 '19 at 10:20
  • 1
    1) that is not what it means, since you can navigate between graphs as well. 2) I don't understand what you are trying to say here 3) we can't answer that. They did state that an activity is meant to be an entry point for your app. But that does not mean there should be only 1 – Tim May 17 '19 at 10:24
  • Assuming that Google isn't pushing for a single activity app, are they suggesting that instead of using the default code for a navigation drawer to build your own navigation drawer that is more inline with the Navigation component? – Johann May 17 '19 at 10:30

2 Answers2

29

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

Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
  • Does his mean I can make a transition from one navigation graph to another in case of activities but they run independently and there is no way I can reference the previous activity's navigation graph to say pop of the current activity and go back to the previous activity? – Anurag Shukla Apr 05 '21 at 04:46
  • Yes, you can perform forward navigation using `navigate` and backward navigation using `navigateUp`. – Srikar Reddy Apr 06 '21 at 03:34
  • 1
    Right, you cannot reference fragments of one navigation graph in the other. They are independent of each other. – Srikar Reddy Apr 06 '21 at 03:34
4

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.

  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

canerkaseler
  • 6,204
  • 45
  • 38
  • Ok but what if i have splash screen and onboarding? Should I use two activities if their themes are different? – deadfish Apr 21 '23 at 17:22
  • In compose you can use different themes for different screens. Hence, you can add your splash and onboarding screens into your one activity. Then, you can define roads and change navigation depends on your business logic. – canerkaseler Apr 24 '23 at 18:58