android 导航不再工作,错误:忽略到目标的popBackStack,因为在当前返回堆栈上找不到它

tyu7yeag  于 2023-02-14  发布在  Android
关注(0)|答案(1)|浏览(237)

突然间,我的应用程序中的导航不知从哪里就不能用了(昨天还能用)。
下面是一个例子:当我从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

这是我的代码片段

用户记录的输入片段

@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
    }
}

导航图

<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>

我在许多其他片段中也有这一点,我使用popupto和popuptoincluse ="true"。我做错了什么?

nmpmafwu

nmpmafwu1#

我找到了解决办法:问题是我不应该弹出到我导航到的同一个目的地,因为这个片段永远不会出现在backstack中。相反,当点击back按钮时,你应该popTo你想要显示的片段。在我的例子中,这是我的startdestination aka. homeFragment:

<action
    android:id="@+id/action_userLogInRegistrationFragment_to_userLoggedInFragment"
    app:destination="@id/userLoggedInFragment"
    app:popUpTo="@id/homeFragment" />

相关问题