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?
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.