简体   繁体   中英

Navigating back with the Navigation Component

in the following, you can see the navigation graph of my first activity hosting some fragments (Fragment A, B & C): 在此处输入图片说明 Here is the XML of this navigation graph:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.celik.abdullah.learningnavigation1.FragmentA"
        android:label="FragmentA"
        tools:layout="@layout/fragment_a">
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB" />
    </fragment>
    <fragment
        android:id="@+id/fragmentB"
        android:name="com.celik.abdullah.learningnavigation1.FragmentB"
        android:label="FragmentB"
        tools:layout="@layout/fragment_b">
        <action
            android:id="@+id/action_fragmentB_to_fragmentC"
            app:destination="@id/fragmentC"
            app:popUpTo="@+id/fragmentB"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/fragmentC"
        android:name="com.celik.abdullah.learningnavigation1.FragmentC"
        android:label="FragmentC"
        tools:layout="@layout/fragment_c">
        <action
            android:id="@+id/action_fragmentC_to_main2Activity"
            app:destination="@id/main2Activity"
            app:popUpTo="@+id/main2Activity"
            app:popUpToInclusive="true" />
    </fragment>
    <activity
        android:id="@+id/main2Activity"
        android:name="com.celik.abdullah.learningnavigation1.Main2Activity"
        android:label="Main2Activity" />
</navigation>

As you can see, from Fragment C there is an action to the second main activity, which holds another set of fragments. For the sake of completeness, I will show you also the navigation graph of that activity: 在此处输入图片说明

Again, here is the XML of the 2nd navigation graph:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation2"
    app:startDestination="@id/fragmentD">

    <fragment
        android:id="@+id/fragmentD"
        android:name="com.celik.abdullah.learningnavigation1.FragmentD"
        android:label="FragmentD"
        tools:layout="@layout/fragment_d">
        <action
            android:id="@+id/action_fragmentD_to_fragmentE"
            app:destination="@id/fragmentE"
            app:popUpTo="@+id/fragmentD"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/fragmentE"
        android:name="com.celik.abdullah.learningnavigation1.FragmentE"
        android:label="FragmentE"
        tools:layout="@layout/fragment_e"/>
</navigation>

When I am in Fragment E, I want to go back to Fragment C. For that I have the following code in Fragment E:

binding.back.setOnClickListener{
            Navigation.findNavController(requireActivity(), R.id.nav_host_fragment).navigateUp()
        }

What I am doing here is that when the back button on Fragment E is clicked, then I find the nav controller of the first navigation graph whose ID is nav_host_fragment and then call navigateUp() . Due to the popUpTo and popUpToInclusive attributes, Fragment C and Fragment D should not be on stack. So, I thought that navigating back to Fragment C from Fragment E could maybe work.

But that approach does not work. The app crashes with the following exception:

2020-03-01 13:00:28.695 16117-16117/com.celik.abdullah.learningnavigation1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.celik.abdullah.learningnavigation1, PID: 16117
    java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
        at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:368)
        at androidx.navigation.Navigation.findNavController(Navigation.java:58)
        at com.celik.abdullah.learningnavigation1.FragmentE$onViewCreated$1.onClick(FragmentE.kt:39)
        at android.view.View.performClick(View.java:6261)
        at android.view.View$PerformClick.run(View.java:23748)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

Has someone a solution for this type of navigation - navigating back & forth between two graphs ?

NOTE: The reason why I have two activities is the difference between the layout of activity 1 & 2. Activity 1's layout has a Bottom Navigation Menu & a Navigation Drawer, and Activity 2's layout has none of them.

Overriding the onBackPressed function in your second activity may do the trick.

override fun onBackPressed()
{
    moveTaskToBack(true)
}

This should finish the second activity, thus moving you back to the first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM