Navigation Architecture Component - Activities

AndroidAndroid Architecture-Navigation

Android Problem Overview


I've been following the docs from [Navigation Architecture Component][1] 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][2] 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.

Android Solutions


Solution 1 - Android

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);

Solution 2 - Android

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)

Solution 3 - Android

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

Kotlin:

val activityNavigator = ActivityNavigator( context!!)
                activityNavigator.navigate(
                    activityNavigator.createDestination().setIntent(
                        Intent(
                            context!!,
                            SecondActivity::class.java
                        )
                    ), null, null, null
                )

Java:

ActivityNavigator activityNavigator = new ActivityNavigator(this);
activityNavigator.navigate(activityNavigator.createDestination().setIntent(new Intent(this, SecondActivity.class)), null, null, null);

nav_graph.xml

<fragment android:id="@+id/fragment"
        android:name="com.codingwithmitch.navigationcomponentsexample.SampleFragment"
        android:label="fragment_sample"
        tools:layout="@layout/fragment_sample">

    <action
            android:id="@+id/action_confirmationFragment_to_secondActivity"
            app:destination="@id/secondActivity" />

</fragment>
<activity
        android:id="@+id/secondActivity"
        android:name="com.codingwithmitch.navigationcomponentsexample.SecondActivity"
        android:label="activity_second"
        tools:layout="@layout/activity_second" />

Kotlin:

lateinit var navController: NavController
navController = Navigation.findNavController(view)
navController!!.navigate(R.id.action_confirmationFragment_to_secondActivity)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionV&#237;ctor AlbertosView Question on Stackoverflow
Solution 1 - AndroidianhanniballakeView Answer on Stackoverflow
Solution 2 - AndroidVíctor AlbertosView Answer on Stackoverflow
Solution 3 - AndroidKarthik KamatchiView Answer on Stackoverflow