我打算像这样实现导航:

我面临的问题是当用户进入LoginFragmennt并按下返回按钮时,它再次加载LognFragment即。陷入循环。
我LoginnFragment根据这个答案导航到使用条件导航。
如何正确实施?
我打算像这样实现导航:

我面临的问题是当用户进入LoginFragmennt并按下返回按钮时,它再次加载LognFragment即。陷入循环。
我LoginnFragment根据这个答案导航到使用条件导航。
如何正确实施?
恕我直言,我在我的应用程序中的操作方式更加简洁。只需在导航图中添加这些设置:
<fragment
android:id="@+id/profile_dest"
android:name="com.example.ProfileFragment">
<action
android:id="@+id/action_profile_dest_to_login_dest"
app:destination="@id/login_dest"
app:popUpTo="@+id/profile_dest"
app:popUpToInclusive="true" />
</fragment>
然后导航到登录通过
findNavController().navigate(R.id.action_profile_dest_to_login_dest).
ProfileFragment当我们导航到时popUpTo 和 popUpToInclusive 关闭,LoginFragment因此如果用户导航回来,它会退出应用程序。
我可以提出的解决方案之一是在您的活动 onBackPressed 方法中覆盖,如果您当前的目的地(在处理后按之前)是登录片段,则完成活动。
override fun onBackPressed() {
val currentDestination=NavHostFragment.findNavController(nav_host_fragment).currentDestination
when(currentDestination.id) {
R.id.loginFragment -> {
finish()
}
}
super.onBackPressed()
}
这是 Ian Lake 在2020 年 7 月 23 日在Android 开发者YouTube 频道上的导航视频中建议的官方解决方案。该解决方案基于导航2.3 版本,该版本引入了将结果返回到先前目的地的能力。
在我们的例子中,登录片段将LOGIN_SUCCESSFUL状态返回到先前的目的地,它可能是配置文件片段或任何其他需要登录的片段。
class LoginFragment : Fragment(R.layout.login) {
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val navController = findNavController()
val savedStateHandle = navController.previousBackStackEntry?.savedStateHandle
?: throw IllegalStateException("the login fragment must not be a start destination")
savedStateHandle.set(LOGIN_SUCCESSFUL, false)
// Hook up your UI, ask for login
userRepository.addLoginSuccessListener {
savedStateHandle.set(LOGIN_SUCCESSFUL, true)
navController.popBackStack()
}
}
}
配置文件片段订阅LOGIN_SUCCESSFUL状态并处理它。请注意,在登录片段放入结果并返回到配置文件片段之前,不会调用观察者 lambda。
class ProfileFragment : Fragment(R.layout.profile) {
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val navController = findNavController()
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
userRepository.userFlow.collect { user ->
if (user == null) {
navController.navigate(R.id.login)
}
}
}
val savedStateHandle = navController.currentBackStackEntry?.savedStateHandle
?: throw IllegalStateException()
savedStateHandle.getLiveData<Boolean>(LOGIN_SUCCESSFUL)
.observe(viewLifecycleOwner) { success ->
if (!success) {
// do whathever we want, just for an example go to
// the start destination which doesn't require login
val startDestination = navController.graph.startDestination
navController.navigate(startDestination, navOptions {
popUpTo(startDestination {
inclusive = true
})
})
}
}
}
}