我正在测试这个新库,如果用户没有登录,我想导航到登录片段。这个检查是在一个基本片段中完成的,比如:
abstract class SignedInFragment : Fragment() {
override fun onResume() {
super.onResume()
if (FirebaseAuth.getInstance().currentUser == null) {
NavHostFragment.findNavController(this /* Fragment */)
.navigate(R.id.action_login)
}
}
}
但这只是以 RuntimeException 结束,说 FragmentManager 已经在执行事务:
java.lang.RuntimeException: Unable to resume activity {me.hammarstrom.loco/me.hammarstrom.loco.MainActivity}: java.lang.IllegalStateException: FragmentManager is already executing transactions
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3645)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
导航图如下所示:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/loginFragment"
android:name="me.hammarstrom.loco.ui.login.LoginFragment"
android:label="LoginFragment" >
<action
android:id="@+id/action_loginFragment_to_signedInFragment"
app:destination="@id/signedInFragment" />
</fragment>
<fragment
android:id="@+id/signedInFragment"
android:name="me.hammarstrom.loco.ui.common.SignedInFragment"
android:label="SignedInFragment" />
<fragment
android:id="@+id/mainFragment"
android:name="me.hammarstrom.loco.ui.main.MainFragment"
android:label="MainFragment" />
<action
android:id="@+id/action_login"
app:destination="@id/loginFragment" />
这种导航应该以其他方式完成吗?我错过了什么吗?