Search code examples
androidandroid-fragmentsandroid-architecture-navigationandroid-navigationbottomsheetdialogfragment

Navigation Component dialog fragment NoSuchElementException


I've 3 fragments in a bottom nav bar

-> with the third fragment having a viewpager2 with 2 fragments in them.

-> Clicking on a button in one of the viewpager's fragments opens a BottomSheetDialogFragment and after completing few steps in that BottomSheetDialogFragment

-> that fragment is dismissed and another BottomSheetDialogFragment is opened.

When the last BottomSheetDialogFragment is closed, the app crashes with the following error

java.util.NoSuchElementException: Collection contains no element matching the predicate.
        at androidx.navigation.fragment.DialogFragmentNavigator$observer$1.onStateChanged(DialogFragmentNavigator.kt:216)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:284)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:302)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.fragment.app.Fragment.performStop(Fragment.java:3179)
        at androidx.fragment.app.FragmentStateManager.stop(FragmentStateManager.java:619)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:297)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1793)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1711)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1654)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:488)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7660)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I upgraded to navigation library 2.4.0-alpha03 a month back and ignored this issue with the thought it's in alpha and would be fixed, alpha05 is out and still the same, I decided to go to the stable version 2.3.5 and the error persists.

Here's the gradle (module)

apply plugin: "androidx.navigation.safeargs.kotlin"

implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0-alpha05'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.0-alpha05'

gradle (module)

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0-alpha05"

Navigation graph looks like this

    <fragment
        android:id="@+id/profile"
        android:name="com.example.user.profile.ProfileFragment"
        android:label="Profile"
        tools:layout="@layout/fragment_profile">
        <action
            android:id="@+id/action_profile_to_editProfileFragment"
            app:destination="@id/editProfileFragment"
            app:enterAnim="@anim/from_right"
            app:exitAnim="@anim/to_left"
            app:popEnterAnim="@anim/from_left"
            app:popExitAnim="@anim/to_right" />
        <action
            android:id="@+id/action_profile_to_predefinedHabitsFragment"
            app:destination="@id/predefinedHabitsFragment" />
        <action
            android:id="@+id/action_profile_to_editHabitBottomSheetFragment"
            app:destination="@id/editHabitBottomSheetFragment" />

    </fragment>

    <dialog
        android:id="@+id/addPredefinedHabitBottomSheet"
        android:name="com.example.habits.add.predefined.AddPredefinedHabitBottomSheet"
        android:label="AddPredefinedHabitBottomSheet"
        tools:layout="@layout/add_predefined_habit_bottom_sheet">
        <argument
            android:name="habitData"
            app:argType="com.example.predefined.models.PredefinedHabitsModelItem" />
    </dialog>

    <dialog
        android:id="@+id/addCustomHabitBottomSheet"
        android:name="com.iku.habits.add.custom.AddCustomHabitBottomSheet"
        android:label="AddCustomHabitBottomSheet"
        tools:layout="@layout/add_custom_habit_bottom_sheet" />

Solution

  • Solution to this described on Google's issue - https://issuetracker.google.com/issues/191073055#comment14