25

I'm learning android development and the navigation component, trying to link multiple activities as the document had written. But it seems like it's impossible to create action between two activities to a single navigation graph which was reasonable to me as the document had written.

The NavController and its navigation graph is contained within a single activity. Therefore, when migrating an existing project to use the Navigation Architecture Component, focus on migrating one Activity at a time by creating a navigation graph for the destinations within each Activity.

So the question is what does the following sentence mean? I could add multiple activities to one navigation graph but couldn't add a link(action) between them.

Separate Activities can then be linked by adding activity destinations to the navigation graph, replacing existing usages of startActivity() throughout the code base.

Jian
  • 3,118
  • 2
  • 22
  • 36
  • 1
    Theoretically, we can create multiple navigation graph files (one per feature module) which only represent the module internal navigation. The navigation between the modules themselves can happen by including the graphs of the feature module to the entry point of the parent module. – aslamhossin Dec 11 '18 at 04:17

1 Answers1

43

Since each NavController and navigation graph is contained within a single activity, an <activity> destination is an exit point from that graph - once you use navigate(R.id.your_activity_destination) to go to the next activity, that NavController and graph is no longer active (it is on the activity on the back stack, not the newly launched activity).

On your second Activity, you would have a second navigation graph with any additional <activity> outbound destinations to go to further activities.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 2
    had to read through 10+ guides on Medium to finally get correct answer :) – Oleksandr Mar 18 '19 at 12:25
  • @Oleksandr - it is also specifically called out in the [Migrate to Navigation documentation](https://developer.android.com/guide/navigation/navigation-migrate#create_a_navigation_graph) – ianhanniballake Mar 18 '19 at 14:26
  • @ianhanniballake I have bottom navigation view and app bar on products listing page, but no bottom navigation view on detail page. How can i achieve that with architecture component. As, whatever fragment i attach to nav graph, it shows bottom navigation view – PRERNA GUPTA Apr 19 '19 at 14:16
  • @ianhanniballake If I navigate to an activity (Activity B), it changes the Activity A for the Activity B. But if I move from graph A (Activity A) to graph B (Activity B), navigation component decides to use Activity A as host for fragments in graph B, even when in graph B it is appearing Activity B as host. Is it a bug or it is intentionally? – JavierSegoviaCordoba Jul 24 '19 at 18:11
  • 1
    @JavierSegoviaCordoba - Sounds like you should post your own question with more details on your navigation graph, but if you have two nested graphs and are navigating between them, then that takes place all within one host (and hence, one activity). – ianhanniballake Jul 24 '19 at 22:29
  • @ianhanniballake I created a new question, https://stackoverflow.com/questions/57192266/navigation-graphs-and-multiple-host-activities, thank you for your time! – JavierSegoviaCordoba Jul 24 '19 at 22:59
  • how to go to previous activity? https://stackoverflow.com/questions/73415269/how-to-return-to-previous-activity-when-using-navigation-component-and-up-naviga – user924 Aug 19 '22 at 10:25