NavController no current navigation node after device rotation

后端 未结 4 933
予麋鹿
予麋鹿 2020-12-31 08:08

I am making app, which supports different device orientations. Navigation is carried out by Android Jetpack\'s Navigation. App main screen for landscape orientation is prese

相关标签:
4条回答
  • 2020-12-31 08:52

    The problem for me was the version of lifecycle dependency

    "androidx.lifecycle:lifecycle-extensions:$lifecycle_version
    

    version '2.2.0' cause the problem, I returned to use '2.1.0'

    lifecycle_version = '2.1.0'
    
    0 讨论(0)
  • 2020-12-31 09:04

    None of these solutions fixed my issue.

    I fixed it by including my nested graph into the main graph.

    I have a nav_main which includes a nav_sub_1.
    nav_sub_1 also includes another sub graph, nav_sub_2
    When I tried to start my activity by setting nav_sub_2, the IllegalStateException occured

    java.lang.IllegalStateException: no current navigation node

    But this would not happen by setting nav_main or nav_sub_1.

    My main graph nav_main looks like this:

    <navigation
        <fragment...>
        <include app:graph="@navigation/nav_sub_1
    <navigation/>
    

    and nav_sub_1:

    <navigation
        <fragment...>
        <include app:graph="@navigation/nav_sub_2
    <navigation/>
    

    I included nav_sub_2 in nav_main and the issue was fixed!

    <navigation
        <fragment...>
        <include app:graph="@navigation/nav_sub_1
        <include app:graph="@navigation/nav_sub_2
    <navigation/>
    
    0 讨论(0)
  • 2020-12-31 09:07

    Thanks to Slav's comment, he was right. I updated navigation module to 2.2.0 navigation_version = '2.2.0' in app's module build.gradle

    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    

    After doing this problem is no longer appears, looks like that was a bug in navigation.

    0 讨论(0)
  • 2020-12-31 09:11

    You can also fix it like this. In your host activity in manifest adding this atribute:

    <activity android:name=".MainActivity"
            android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"
    

    But the best way is change your dependencies for navigation from:

    implementation "android.arch.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "android.arch.navigation:navigation-ui-ktx:$navigation_version"
    

    to

    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    
    0 讨论(0)
提交回复
热议问题