And*_*rew 5 android kotlin android-navigation android-jetpack android-architecture-navigation
突然,我的应用程序中的导航突然无法正常工作(昨天确实有效)。
这是一个示例:当我从 导航userLoggedInFragment到 时userLoggedOutFragment,使用参数app:popUpTo="@id/userLoggedOutFragment" app:popUpToInclusive="true",然后单击后退按钮,即使我使用了 popUpTo,它仍然会将我导航回 userLoggedInFragment。然后我得到信息:
I/NavController: Ignoring popBackStack to destination com.example.app:id/userLoggedOutFragment as it was not found on the current back stack
Run Code Online (Sandbox Code Playgroud)
这是我的片段代码
@AndroidEntryPoint
class UserLoggedInFragment : Fragment() {
private var _binding: FragmentUserLoggedInBinding? = null
private val binding: FragmentUserLoggedInBinding get() = _binding!!
private val viewModel: LogoutViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentUserLoggedInBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
binding.btnLogout.btnNext.setOnClickListener {
findNavController().navigate(
UserLoggedInFragmentDirections.actionUserLoggedInFragmentToUserLoggedOutFragment()
)
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Run Code Online (Sandbox Code Playgroud)
<fragment
android:id="@+id/userLoggedInFragment"
android:name="com.example.app.framework.ui.view.fragment.user.UserLoggedInFragment"
tools:layout="@layout/fragment_user_logged_in">
<action
android:id="@+id/action_userLoggedInFragment_to_userLoggedOutFragment"
app:destination="@id/userLoggedOutFragment"
app:popUpTo="@id/userLoggedOutFragment"
app:popUpToInclusive="true" />
</fragment>
Run Code Online (Sandbox Code Playgroud)
我在使用 popUpTo 和 popUpToIncluse="true" 的许多其他片段中也有这个。我究竟做错了什么??
我找到了解决方案:问题是我不应该弹出到我正在导航到的同一目的地,因为片段永远不会在后堆栈中。相反,您应该在单击后退按钮时 popTo 您想要显示的片段。就我而言,这将是我的起点。主页片段:
<action
android:id="@+id/action_userLogInRegistrationFragment_to_userLoggedInFragment"
app:destination="@id/userLoggedInFragment"
app:popUpTo="@id/homeFragment" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6590 次 |
| 最近记录: |