应用程序在后台时无法导航

Иль*_*нин 6 android android-jetpack android-architecture-navigation

我使用导航组件在片段之间导航。

在主要活动中初始化导航:

val navHostFragment = NavHostFragment.create(R.navigation.nav_graph)
supportFragmentManager
        .beginTransaction()
        .replace(R.id.container, navHostFragment)
        .commit()
Run Code Online (Sandbox Code Playgroud)

导航到飞溅片段中的另一个片段:

findNavController().navigate(SplashFragmentDirections.actionSplashFragmentToHomeActivity())
Run Code Online (Sandbox Code Playgroud)

在飞溅中我进行身份验证,需要一些时间,身份验证后我导航到主页片段。如果我最小化启动片段中的应用程序(按主页按钮),则导航()不起作用,当我再次打开应用程序时,它仍保留在启动片段中

在控制台中:

NavController: Ignoring popBackStack to destination my.app/homeFragment as it was not found on the current back stack
FragmentNavigator: Ignoring navigate() call: FragmentManager has already saved its state
Run Code Online (Sandbox Code Playgroud)

小智 1

当您收到身份验证回调时,测试您的应用程序是否位于前台,如果是,则导航。如果您的应用程序在后台运行,则不执行任何操作。

在闪屏的 onResume 方法中,测试您的用户是否已通过身份验证。如果是这样,请跳过启动画面。

  • 是的,我就是这么做的。但我认为这是一种糟糕且愚蠢的方式。 (3认同)