Android 导航弹出窗口未按预期工作
我无法从导航后台清除启动页。在navigation.xml文件中,您可以看到我将Android 导航弹出窗口未按预期工作,android,Android,我无法从导航后台清除启动页。在navigation.xml文件中,您可以看到我将popUpTo属性设置为初始页面,同时将popuptinclusive设置为true,但这似乎没有效果 当应用程序打开时,启动屏幕显示一秒钟,然后显示主_片段,但带有一个功能性返回箭头 我错过了什么 activity_main.xml <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_fragm
popUpTo
属性设置为初始页面,同时将popuptinclusive
设置为true
,但这似乎没有效果
当应用程序打开时,启动屏幕显示一秒钟,然后显示主_片段,但带有一个功能性返回箭头
我错过了什么
activity_main.xml
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:navGraph="@navigation/navigation" />
navigation.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/splashFragment">
<fragment
android:id="@+id/splashFragment"
android:name="org.chrisolsen.myapplication.SplashFragment"
android:label="fragment_splash"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/action_splashFragment_to_mainFragment"
app:destination="@id/mainFragment"
app:popUpTo="@id/splashFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/mainFragment"
android:name="org.chrisolsen.myapplication.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" />
</navigation>
当您从后堆栈弹出起始目标并使用
setupWithNavController()
时,这是预期的行为
根据,您不应该从后堆栈中弹出起始目标。对于初始屏幕,您的活动应该实现(不应该有单独的活动或单独的片段)
您的弹出窗口
工作正常-这就是为什么系统后退按钮工作正常并导致您直接退出应用程序的原因。但是,所有NavigationUI
和setupWithNavController
api都假定您遵循导航原则。通过弹出起始目标,您触发的行为与以下情况相同:
在这种情况下,“后退”按钮会将您带回上一个应用程序,而“向上”按钮会在导航图中的分层父目标上启动应用程序的任务
但您的“上一个应用程序”是您的启动程序。根据,您不应该使用启动片段作为您的启动目标。将其分离到自己的活动中是最佳做法吗?您不应该在启动屏幕中使用活动或片段,您应该使用。感谢您的输入。在你发布的链接中,这是一个与自己的主题一起使用的活动,我是否遗漏了什么?如果原始帖子不是启动屏幕或任何其他条件屏幕,我仍然想知道为什么没有使用导航操作中设置的选项从后堆栈中清除第一个片段。这些选项对后堆栈中的第一项不起作用吗?我想你会发现系统返回按钮不会返回到初始片段(它只是退出应用程序)。我说的是动作栏中的“向上”按钮,而不是“系统后退”按钮,对吗?
class SplashFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
GlobalScope.launch {
withContext(Dispatchers.Main) {
delay(1000)
findNavController().navigate(SplashFragmentDirections.actionSplashFragmentToMainFragment())
}
}
return inflater.inflate(R.layout.fragment_splash, container, false)
}
}
<?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/splashFragment">
<fragment
android:id="@+id/splashFragment"
android:name="org.chrisolsen.myapplication.SplashFragment"
android:label="fragment_splash"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/action_splashFragment_to_mainFragment"
app:destination="@id/mainFragment"
app:popUpTo="@id/splashFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/mainFragment"
android:name="org.chrisolsen.myapplication.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" />
</navigation>