使用导航架构组件时后退按钮不起作用
Back button not working when using Navigation architecture component
我有以下 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)
}
}
具有以下布局:
<?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>
我还有两个片段:Fragment1 和Fragment2。片段 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)
}
}
其布局为:
<?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>
片段 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)
}
}
具有以下布局:
<?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>
最后但并非最不重要的是 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>
如您所见,这里没有什么特别之处。我唯一要做的就是导航到 Fragment1 的 onViewCreated
中的 Fragment2。
当我 运行 时,应用 Fragment2 按预期显示。当我按下后退按钮时,我希望 return 到 Fragment1,但这并没有发生。就好像后退按钮被禁用并且按下它不会做任何事情。我必须按主页按钮才能退出应用程序。
报告行为的原因是您的代码模拟了无限循环。
你不应该打电话给
findNavController().navigate(R.id.action_fragment1_to_fragment2)
从 onViewCreated() 改为使用按钮或任何形式的触发器来执行导航。
您的流量:
第 1 步:在应用启动时调用 fragment1 的 onViewCreated() 并将应用导航到 fragment2。
第 2 步:一旦应用程序导航到 fragment2,fragment1 视图就会被销毁。即 onDistroyView() 在 fragment1 上被调用。
第 3 步:在您从 fragment2 按下后退按钮的那一刻,fragment1 被从返回堆栈中拉出,因为它的视图被销毁,它再次经历视图创建周期。
即再次调用 onViewCreated() 。结果,您的应用陷入无限循环。
我有以下 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)
}
}
具有以下布局:
<?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>
我还有两个片段:Fragment1 和Fragment2。片段 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)
}
}
其布局为:
<?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>
片段 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)
}
}
具有以下布局:
<?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>
最后但并非最不重要的是 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>
如您所见,这里没有什么特别之处。我唯一要做的就是导航到 Fragment1 的 onViewCreated
中的 Fragment2。
当我 运行 时,应用 Fragment2 按预期显示。当我按下后退按钮时,我希望 return 到 Fragment1,但这并没有发生。就好像后退按钮被禁用并且按下它不会做任何事情。我必须按主页按钮才能退出应用程序。
报告行为的原因是您的代码模拟了无限循环。 你不应该打电话给
findNavController().navigate(R.id.action_fragment1_to_fragment2)
从 onViewCreated() 改为使用按钮或任何形式的触发器来执行导航。
您的流量:
第 1 步:在应用启动时调用 fragment1 的 onViewCreated() 并将应用导航到 fragment2。
第 2 步:一旦应用程序导航到 fragment2,fragment1 视图就会被销毁。即 onDistroyView() 在 fragment1 上被调用。
第 3 步:在您从 fragment2 按下后退按钮的那一刻,fragment1 被从返回堆栈中拉出,因为它的视图被销毁,它再次经历视图创建周期。 即再次调用 onViewCreated() 。结果,您的应用陷入无限循环。