Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link multiple activities in android navigation editor?

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.

like image 615
theJian Avatar asked Dec 11 '18 03:12

theJian


People also ask

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.

What is NavController in Android?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.


1 Answers

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.

like image 187
ianhanniballake Avatar answered Oct 17 '22 19:10

ianhanniballake