Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Start Second Activity with Navigation in Jetpack

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?

like image 958
AndroidEduIO Avatar asked May 30 '18 08:05

AndroidEduIO


People also ask

How do you navigate between screens in jetpack compose?

Navigation is then performed by making calls to the navigate() method of the navigation controller instance, passing through the path of the destination composable. Compose also supports the passing of arguments to the destination composable.

Can we use navigation component for activity?

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.


1 Answers

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.

like image 104
egonzal Avatar answered Oct 16 '22 22:10

egonzal