清除导航返回堆栈并重新创建 activity

Clear Navigation back stack and Recreating activity

我正在开发一个具有 LoginFragmentHomeFragment 的应用程序。 我正在使用 jetpack 导航组件来处理片段导航。 首先,在该用户在 LoginFragment 中完成登录过程后,我调用 popBackStack(),然后将用户导航到 HomeFragment。 当用户单击 dark mode 按钮时出现问题。 我在 activity 上打电话给 recreate()。 我需要再次恢复 NavHost 但我清除了那个 navHost 的 StartDestination

我该怎么做才能解决这个问题?

注意:我试过使用popUpTo;但这对我不起作用。

根据 Principles of Navigation:

,您不应使用登录作为图表的起始目的地

Note: An app might have a one-time setup or series of login screens. These conditional screens should not be considered start destinations because users see these screens only in certain cases.

相反,请遵循 guide for doing Login in the Navigation Component 并有条件地导航到您的登录屏幕(即,仅当用户未登录时)。