简体   繁体   English

使用导航架构组件时后退按钮不起作用

[英]Back button not working when using Navigation architecture component

I have the following Activity:我有以下活动:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

with the following layout:具有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/navHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

I also have two fragments: Fragment1 and Fragment2.我还有两个片段:Fragment1 和 Fragment2。 Fragment1 is:片段1是:

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController

class Fragment1 : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_1, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        findNavController().navigate(R.id.action_fragment1_to_fragment2)
    }

}

and its layout is:它的布局是:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment1">

    <TextView
        android:id="@+id/toFragment2Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Fragment 1" />

</FrameLayout>

Fragment2 is:片段2是:

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class Fragment2 : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_2, container, false)
    }

}

with the following layout:具有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment2">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Fragment 2" />

</FrameLayout>

and last but not least is the contents of navigation_graph.xml最后但并非最不重要的是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"
    app:startDestination="@id/fragment1">

    <fragment
        android:id="@+id/fragment1"
        android:name="ir.fallahpoor.navigation.Fragment1"
        android:label="fragment_1"
        tools:layout="@layout/fragment_1">
        <action
            android:id="@+id/action_fragment1_to_fragment2"
            app:destination="@id/fragment2" />
    </fragment>
    <fragment
        android:id="@+id/fragment2"
        android:name="ir.fallahpoor.navigation.Fragment2"
        android:label="fragment_2"
        tools:layout="@layout/fragment_2" />
</navigation>

As you can see there is nothing fancy here.如您所见,这里没有什么花哨的。 The only thing that I'm doing is navigating to Fragment2 in onViewCreated of Fragment1.我唯一要做的就是导航到 Fragment1 的onViewCreated中的 Fragment2。

When I run the app Fragment2 is displayed as expected.当我运行应用程序 Fragment2 时按预期显示。 When I press the back button I expect to return to Fragment1, but that doesn't happen.当我按下后退按钮时,我希望返回到 Fragment1,但这并没有发生。 It is as if back button is disabled and pressing it doesn't do anything.就好像后退按钮被禁用并且按下它不会做任何事情。 I have to press the home button to exit the app.我必须按主页按钮才能退出应用程序。

The Reason for the reported behavior is your code simulated an infinite loop.报告行为的原因是您的代码模拟了无限循环。 You should not call你不应该打电话

findNavController().navigate(R.id.action_fragment1_to_fragment2)

from onViewCreated() instead use a button or any form of trigger to perform the navigation.从 onViewCreated() 改为使用按钮或任何形式的触发器来执行导航。

Your Flow:您的流程:

STEP 1: onViewCreated() of fragment1 is called when the app launched and the app is navigated to fragment2.第 1 步:在应用启动时调用 fragment1 的 onViewCreated(),并将应用导航到 fragment2。

STEP 2: As soon as the application navigates to fragment2, the fragment1 view is destroyed.第 2 步:一旦应用程序导航到 fragment2,fragment1 视图就会被销毁。 ie onDistroyView() is called on fragment1.即在fragment1 上调用onDistroyView()。

STEP 3: The moment you hit the back button from fragment2, fragment1 is pulled from the back stack, since its view is destroyed, it goes through the view creation cycle again.第 3 步:当您从 fragment2 按下返回按钮时,fragment1 从返回堆栈中拉出,由于它的视图被销毁,它再次进入视图创建周期。 ie onViewCreated() is called again.即再次调用 onViewCreated()。 as a result, your app is stuck with infinite loop.结果,您的应用程序陷入了无限循环。

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

相关问题 Android 架构组件导航:工具栏后退按钮丢失,后退不起作用 - Android architecture component navigation: toolbar back button missing, back not working 如何使用导航架构组件向后导航或向上导航添加日志记录? - How to add logging to navigation back or up using navigation architecture component? Android导航架构组件-系统的后退按钮退出应用 - Android navigation architecture component - system's back button exits the app 使用导航组件时如何覆盖工具栏中后退按钮的行为 - How to override the behavior of back button in toolbar when using navigation component 系统后退按钮在导航组件中不起作用 - System back button not working in Navigation Component Android 导航组件后退按钮不起作用 - Android Navigation Component back button not working 使用导航架构组件时动画错误 - Wrong animation when using navigation architecture component 使用导航抽屉时,工具栏后退按钮不起作用 - Toolbars Back button not working when using with navigation drawer 当使用导航架构操作点击返回按钮时,如何避免片段重新生成? - How to avoid fragment recreation when tap back button using navigation architecture actions? 将底部菜单栏与导航架构组件一起使用时如何从工具栏中删除后退按钮 - How to remove back button from toolbar when using bottom menu bar with navigation architecture components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM