简体   繁体   English

使用导航组件向后导航

[英]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):在下面,您可以看到我托管一些片段(片段 A、B 和 C)的第一个活动的导航图: 在此处输入图片说明 Here is the XML of this navigation graph:这是此导航图的 XML:

<?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.正如你所看到的,从片段 C 有一个动作到第二个主要活动,它包含另一组片段。 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:

<?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:当我在片段 E 中时,我想回到片段 C。为此,我在片段 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() .我在这里做的是,当单击Fragment E上的后退按钮时,我会找到 ID 为nav_host_fragment的第一个导航图的导航控制器,然后调用navigateUp() Due to the popUpTo and popUpToInclusive attributes, Fragment C and Fragment D should not be on stack.由于popUpTopopUpToInclusive属性, Fragment CFragment D不应在堆栈上。 So, I thought that navigating back to Fragment C from Fragment E could maybe work.所以,我认为从Fragment E导航回Fragment C可能会奏效。

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.注意:我有两个活动的原因是活动 1 和活动 2 的布局不同。活动 1 的布局有一个底部导航菜单和一个导航抽屉,而活动 2 的布局没有它们。

Overriding the onBackPressed function in your second activity may do the trick.在您的第二个活动中覆盖 onBackPressed 函数可能会起作用。

override fun onBackPressed()
{
    moveTaskToBack(true)
}

This should finish the second activity, thus moving you back to the first.这应该完成第二个活动,从而使您回到第一个活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航返回时的 RecyclerView(导航组件) - RecyclerView when navigating back (Navigation Component) 使用导航组件导航到自己时如何显示后退按钮? - How to display back button when navigating to self with navigation component? 从 D - 导航组件返回时防止破坏(或恢复状态)片段 B - Prevent destroying (or restore state) of Fragment B when navigating back from D - Navigation Component 为什么我的 android 应用程序在使用后退按钮导航和从 Internet 加载数据时会跳帧? (导航组件) - Why my android app is skipping frames when navigating with back button and loading data fron internet? (Navigation component) 使用导航组件时按下后退按钮退出应用程序而不是导航到上一个屏幕 - Pressing back button exits the app instead of navigating to the previous screen while using navigation component 使用导航组件导航到首选项片段 - Navigating to preference fragment using navigation component 带有底部导航视图和导航组件的后退导航 - Back navigation with bottom navigation view and navigation component 导航时导航主机变为空(Android 导航组件) - Navigation Host becomes null while navigating (Android Navigation Component) 导航组件 NavDeepLinkBuilder 回栈 - Navigation Component NavDeepLinkBuilder back stack 按钮返回带有导航组件的 startDestination - button back in startDestination with navigation component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM