Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Architecture Component - Activities

I've been following the docs from Navigation Architecture Component to understand how this new navigation system works.

To go/back from one screen to another you need a component which implements NavHost interface.

The NavHost is an empty view whereupon destinations are swapped in and out as a user navigates through your app.

But, it seems that currently only Fragments implement NavHost

The Navigation Architecture Component’s default NavHost implementation is NavHostFragment.

So, my questions are:

  • Even if I have a very simple screen which can be implemented with an Activity, in order to work with this new navigation system, a Fragment needs to be hosted containing the actual view?

  • Will Activity implement NavHost interface in a near future?

--UPDATED--

Based on ianhanniballake's answer, I understand that every activity contains its own navigation graph. But if I want to go from one activity to another using the nav component (replacing "old" startActivity call), I can use activity destinations. What is activity destinations is not clear to me because the docs for migration don't go into any detail:

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

  • Is there any benefit on using ActivityNavigator instead of startActivity?
  • What is the proper way to go from activities when using the nav component?
like image 866
Víctor Albertos Avatar asked May 21 '18 15:05

Víctor Albertos


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.

What is navigation architecture component?

Among other goodies, Android Jetpack comes with the Navigation architecture component, which simplifies the implementation of navigation in Android apps. In this tutorial, you'll add navigation to a simple book-searching app and learn about: Navigation graphs and nested graphs. Actions and destinations.


2 Answers

The navigation graph only exists within a single activity. As per the Migrate to Navigation guide, <activity> destinations can be used to start an Activity from within the navigation graph, but once that second activity is started, it is totally separate from the original navigation graph (it could have its own graph or just be a simple activity).

You can add an Activity destination to your navigation graph via the visual editor (by hitting the + button and then selecting an activity in your project) or by manually adding the XML:

<activity     android:id="@+id/secondActivity"     android:name="com.example.SecondActivity" /> 

Then, you can navigate to that activity (i.e., start the activity) by using it just like any other destination:

Navigation.findNavController(view).navigate(R.id.secondActivity); 
like image 53
ianhanniballake Avatar answered Sep 17 '22 02:09

ianhanniballake


I managed to navigate from one activity to another without hosting a Fragment by using ActivityNavigator.

ActivityNavigator(this)                     .createDestination()                     .setIntent(Intent(this, SecondActivity::class.java))                     .navigate(null, null) 
like image 31
Víctor Albertos Avatar answered Sep 17 '22 02:09

Víctor Albertos