当手机屏幕关闭时,Android导航架构无法进行导航

7
在我的项目中,我有以下架构: MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"
        app:defaultNavHost="true"
        />

</android.support.constraint.ConstraintLayout>

nav_graph 的设计:

enter image description here

nav_graph 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph"
    app:startDestination="@id/loginFragment">

    <fragment
        android:id="@+id/loginFragment"
        android:name="com.example.LoginFragment"
        android:label="LoginFragment" >
        <action
            android:id="@+id/loginToContentAction"
            app:destination="@id/contentFragment" />
    </fragment>
    <fragment
        android:id="@+id/contentFragment"
        android:name="com.example.ContentFragment"
        android:label="ContentFragment" />
</navigation>

LoginFragment中,我有以下逻辑:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    if(presenter.isUserLogged()) {
        // getNav() returns NavController from the MainActivity
        getNav().navigateTo(R.id.loginToContentAction)
        return
    }

    // init login views etc
}

如果手机屏幕处于开启状态,它就能够完美地工作,但是(例如)如果我通过Android Studio部署构建,并且手机屏幕处于关闭状态,则不会导航到ContentFragment,而是停留在LoginFragment中。我对情况进行了调试,并且代码进入NavController.navigate(R.id.loginToContentAction)并步入其中,但实际上它并没有导航。有什么想法可能导致这种情况?


2
当屏幕关闭时,活动通常会暂停,或者如果屏幕关闭,则会被暂停。它可能在奇怪的点上暂停,从而破坏了流程。 - TheWanderer
1
我也遇到了同样的问题。你找到解决方法了吗? - Julio E. Rodríguez Cabañas
1
我也遇到了同样的问题。我尝试获取部分唤醒锁,但没有起作用。在我的情况下,当一个快速进程在后台结束时发生导航,但如果用户在此期间按下电源按钮,则会调用导航但不会发生任何事情。 - Miguel Sesma
有答案吗? - Zion Aronov
2个回答

0

我正处于同样的情况。我还没有找到一个干净的解决方案,对我来说唯一的解决方法是如果屏幕关闭,则跳过导航。然后在“onStart”中,我执行之前跳过的导航。


0

可能,我已经解决了这个问题。

lifecycleScope.launchWhenResumed {
    findNavController().navigate( R.id.YOUR_ACTION_ID)
}

此外,该扩展函数可用于在片段处于启动状态时延迟执行任何任务。

此扩展函数可用于在屏幕打开后或正常情况下延迟导航。

fun Fragment.doWithDelay(delay: Long, performTask: () -> Unit) {
    Timer().schedule(timerTask {
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                if (!this@doWithDelay.isDetached && this@doWithDelay.isAdded)
                    this@doWithDelay.activity?.runOnUiThread { performTask.invoke() }
            }
        }
    }, delay)
}

使用方法:

 doWithDelay(YOUR_REQUIRED_DELAY) {
       findNavController().navigate(R.id.YOUR_NAVIGATION_ACTION_ID)
 }

对我来说它工作得很好。如果我错了,请指出我的错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接