Navigation not working anymore, Error: Ignoring popBackStack to destination as it was not found on the current back stack
Navigation not working anymore, Error: Ignoring popBackStack to destination as it was not found on the current back stack
突然间,我的应用程序中的导航突然无法正常工作了(昨天确实有效)。
这是一个示例:当我从 userLoggedInFragment
导航到 userLoggedOutFragment
时,使用参数 app:popUpTo="@id/userLoggedOutFragment" app:popUpToInclusive="true"
然后单击后退按钮,它仍然将我导航回 userLoggedInFragment 即使我使用 popUpTo。然后我得到信息:
I/NavController: Ignoring popBackStack to destination com.example.app:id/userLoggedOutFragment as it was not found on the current back stack
这是我的片段代码
UserLoggedInFragment
@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" 的许多其他片段中也有这个。我做错了什么??
我找到了解决方案:问题是我不应该弹出到我导航到的同一个目的地,因为片段永远不会在后台堆栈中。相反,您应该在单击后退按钮时弹出您想要显示的片段。就我而言,这将是我的开始目的地。主页片段:
<action
android:id="@+id/action_userLogInRegistrationFragment_to_userLoggedInFragment"
app:destination="@id/userLoggedInFragment"
app:popUpTo="@id/homeFragment" />
突然间,我的应用程序中的导航突然无法正常工作了(昨天确实有效)。
这是一个示例:当我从 userLoggedInFragment
导航到 userLoggedOutFragment
时,使用参数 app:popUpTo="@id/userLoggedOutFragment" app:popUpToInclusive="true"
然后单击后退按钮,它仍然将我导航回 userLoggedInFragment 即使我使用 popUpTo。然后我得到信息:
I/NavController: Ignoring popBackStack to destination com.example.app:id/userLoggedOutFragment as it was not found on the current back stack
这是我的片段代码
UserLoggedInFragment
@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" 的许多其他片段中也有这个。我做错了什么??
我找到了解决方案:问题是我不应该弹出到我导航到的同一个目的地,因为片段永远不会在后台堆栈中。相反,您应该在单击后退按钮时弹出您想要显示的片段。就我而言,这将是我的开始目的地。主页片段:
<action
android:id="@+id/action_userLogInRegistrationFragment_to_userLoggedInFragment"
app:destination="@id/userLoggedInFragment"
app:popUpTo="@id/homeFragment" />