问题
I'm using Android's AAC Navigation Component and have implemented conditional navigation for my login flow as described in this post: Navigation Architecture Component - Login screen
Now everything appears to be working, however after being navigated to the login screen and successfully logging in then navigating back to the main screen, on rotation the app crashes with the following error:
--------- beginning of crash
2019-02-02 13:08:28.423 6030-6030/uk.co.victoriajanedavis.chatapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.co.victoriajanedavis.chatapp, PID: 6030
java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.victoriajanedavis.chatapp/uk.co.victoriajanedavis.chatapp.presentation.ui.main.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4699)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
Caused by: java.lang.IllegalStateException: unknown destination during restore: uk.co.victoriajanedavis.chatapp:id/friendsFragment
at androidx.navigation.NavController.onGraphCreated(NavController.java:483)
at androidx.navigation.NavController.setGraph(NavController.java:459)
at androidx.navigation.NavController.setGraph(NavController.java:424)
at androidx.navigation.NavController.setGraph(NavController.java:406)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:226)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:844)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1129)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3132)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:133)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:345)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:324)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:141)
at uk.co.victoriajanedavis.chatapp.presentation.ui.main.MainActivity.onCreate(MainActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4699)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
2019-02-02 13:08:28.423 6030-6030/uk.co.victoriajanedavis.chatapp E/AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Here is my nav_graph.xml (the id's responsible for the login flow are @+id/action_friendsFragment_to_loginFlowGraph, @+id/action_loginFragment_to_chatFlowGraph, @+id/action_signupFragment_to_chatFlowGraph):
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/chatFlowGraph">
<!-- Chat Flow (nested graph) -->
<navigation android:id="@+id/chatFlowGraph"
app:startDestination="@id/friendsFragment">
<fragment
android:id="@+id/friendsFragment"
android:name="uk.co.victoriajanedavis.chatapp.presentation.ui.friends.FriendRequestsToolbarFragment"
android:label="fragment_friends"
tools:layout="@layout/fragment_friend_requests_toolbar">
<action
android:id="@+id/action_friendsFragment_to_loginFlowGraph"
app:destination="@id/loginFlowGraph"
app:popUpTo="@id/chatFlowGraph"
app:popUpToInclusive="true"
app:enterAnim="@anim/slide_in_bottom"
app:exitAnim="@anim/fade_out"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/slide_out_bottom"/>
<action
android:id="@+id/action_friendsFragment_to_chatFragment"
app:destination="@id/chatFragment"/>
<action
android:id="@+id/action_friendsFragment_to_friendRequestsFragment"
app:destination="@id/friendRequestsFragment"
app:enterAnim="@anim/grow_in_top_right"
app:exitAnim="@anim/fade_out"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/grow_out_top_right"/>
</fragment>
<fragment
android:id="@+id/chatFragment"
android:name="uk.co.victoriajanedavis.chatapp.presentation.ui.chat.ChatFragment"
android:label="fragment_chat"
tools:layout="@layout/fragment_chat" />
</navigation>
<!-- Login/Register Flow (nested graph) -->
<navigation android:id="@+id/loginFlowGraph"
app:startDestination="@id/loginFragment">
<fragment
android:id="@+id/loginFragment"
android:name="uk.co.victoriajanedavis.chatapp.presentation.ui.login.LoginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login">
<action
android:id="@+id/action_loginFragment_to_chatFlowGraph"
app:destination="@id/chatFlowGraph"
app:popUpTo="@id/loginFlowGraph"
app:popUpToInclusive="true"/>
<action
android:id="@+id/action_loginFragment_to_signupFragment"
app:destination="@id/signupFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"/>
</fragment>
<fragment
android:id="@+id/signupFragment"
android:name="uk.co.victoriajanedavis.chatapp.presentation.ui.signup.SignupFragment"
android:label="fragment_signup"
tools:layout="@layout/fragment_signup">
<action
android:id="@+id/action_signupFragment_to_chatFlowGraph"
app:destination="@id/chatFlowGraph"
app:popUpTo="@id/loginFlowGraph"
app:popUpToInclusive="true"/>
</fragment>
</navigation>
<!-- FriendRequestsFragment -->
<fragment
android:id="@+id/friendRequestsFragment"
android:name="uk.co.victoriajanedavis.chatapp.presentation.ui.friendrequests.FriendRequestsFragment"
android:label="fragment_friend_requests"
tools:layout="@layout/fragment_friend_requests">
<action
android:id="@+id/action_friendRequestsFragment_to_sendFriendRequestFragment"
app:destination="@id/sendFriendRequestFragment"
app:enterAnim="@anim/slide_in_bottom"
app:exitAnim="@anim/fade_out"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/slide_out_bottom"/>
</fragment>
<!-- SendFriendRequestFragment -->
<fragment
android:id="@+id/sendFriendRequestFragment"
android:name="uk.co.victoriajanedavis.chatapp.presentation.ui.friendrequests.sent.send.SendFriendRequestFragment"
android:label="fragment_send_friend_request"
tools:layout="@layout/fragment_send_friend_request" />
</navigation>
EDIT: using the latest navigation component version 1.0.0-alpha11
回答1:
This bug was fixed in version 1.0.0-beta01 of the navigation component.
回答2:
Looks like this bug still existed even after 1.0.0-beta01
From the changelog, we can see it solved in 2.1.0-alpha04
来源:https://stackoverflow.com/questions/54496008/android-navigation-component-java-lang-illegalstateexception-unknown-destinati