26

You know It's official now: Google officially recommends single activity app architecture. But there is a difficulty here. We have multiple activities. So when I want to implement Navigation with multiple activities, but I failed.

They said: In cases where multiple Activities share the same layout, the navigation graphs can be combined, replacing navigate calls to the activity destination to navigate calls directly between the two navigation graphs. in here

So I create this :

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@+id/nav_graph_firstActvity">

<activity
    android:id="@+id/nav_graph_firstActvity"
    android:name="io.androidedu.FirstActivity"
    android:label="First Activity">

    <action
        android:id="@+id/nav_graph_actFirstActvity"
        app:destination="@id/nav_graph_secondActvity" />
</activity>

<activity
    android:id="@+id/nav_graph_secondActvity"
    android:name="io.androidedu.SecondActivity"
    android:label="Second Activity" />

After that i can not find any sample for multiple activities in here. There is some sample like that :

Navigation.findNavController(view).navigate(R.id.nav_graph_actFirstActvity)

But findNavController() wait for a view, not an activity.

How can I solve this folks?

Mauker
  • 11,237
  • 7
  • 58
  • 76
DummytoDummy
  • 553
  • 1
  • 5
  • 13
  • why do you need to navigate to first activity if it's already startDestination.? And what do you mean by " wait a view, not an activity"? – Man Dec 16 '18 at 20:25

2 Answers2

13

Navigation is meant to help the Navigation on Fragments, as they mention it in the note in blue here

Note: The Navigation Architecture Component is designed for apps that have one main activity with multiple fragment destinations. The main activity “hosts” the navigation graph. In an app with multiple activity destinations, each additional activity hosts its own navigation graph. Modifying an activity to host navigation is discussed later in this document.

So what you can do is to use an Activity as a destination in your nav_graph1.xml and that Activity(the destination) has to have its own nav_graph2.xml. This way you keep using Navigation as a way to go through your app.

It's true the way the google documentation you mention when using multiple Activities that shares same layout, it is a bit confusing. But I think what they mean is that you can merge the Activity1 with Fragment1 (nav_graph1.xml) and Activity2 with Fragment2 (nav_graph2.xml), into Activity3 with (Fragment1 and Fragment2) since they share the same layout, and you can use nav_graph.xml pointing to nav_graph2.xml

Hope it helps

Update:

Navigation.findNavController(view).navigate(R.id.nav_graph_actFirstActvity)

The view can be any view that is inside the layout that contains the NavHostFragment. It will search for the corresponding nav_graph.xml that corresponds to that view or all its parents.

egonzal
  • 699
  • 4
  • 10
  • last edit is december 2018, and i'm pretty sure I read somewhere that communicating between activities is a thing now with jetpack navigation. I just can't find where I read that. Does anyone know more about this issue? – Quentin vk Jun 30 '20 at 12:34
  • 3
    Actually just changing the tag to in navigation graph (and adding activity to manifest of course) works in late versions as @Lawnio mentioned – guy_m Jul 12 '20 at 21:49
  • Yes, you can go to an but I want to go to a specific destination inside that activity's navigation graph! – RumburaK Jan 30 '21 at 15:16
  • 1
    To go to a specific fragment inside an activity's graph you use the NavDeepLinkBuilder: NavDeepLinkBuilder(this@SubscriptionActivity).apply { setComponentName(DashboardActivity::class.java) setGraph(R.navigation.dashboard_nav_graph) setDestination(R.id.nav_home) createPendingIntent().send() } – alfietap May 28 '21 at 08:37
-1

I found that using actions to navigate to an activity will give you the error:

IllegalArgumentException: navigation destination xxx is unknown to this NavController

You can use directions like this:

    findNavController().navigate(directions)
Eric B.
  • 673
  • 11
  • 16