Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Android Navigation Component not working with Dialog Fragments

Posted on November 11, 2022 By

Solution 1 :

AlertDialog automatically dismisses the Dialog (and hence, removes your DialogFragment) when the callback you add to setPositiveButton is fired. Because you’re doing work asynchronously, your databaseSavingCompleted method is called after the DialogFragment is destroyed, detached from the FragmentManager, and removed from the NavController – you’re leaking a reference to your DialogFragment (as it would otherwise be garbage collected).

Therefore when NavHostFragment.findNavController(this) fires, all hooks that would let it access the NavController are already cleaned up.

If you don’t want your button to immediately dismiss the dialog, you need to pass in null to setPositiveButton() and instead get a reference to the button after the dialog has been created by calling its getButton() API and manually setting an OnClickListener that would kick off your AsyncTask (and disable the button to prevent it from being clicked more than once).

Problem :

Disclaimer: I’ve checked the documentation and since 2.1.0 the navigation components has supported Dialog Fragments. (https://developer.android.com/jetpack/androidx/releases/navigation#2.1.0)

Error That I’m Getting

I’m getting this error when trying to go from a DialogFragment to my Start Destination:

java.lang.IllegalStateException: Fragment PostDistressDialog{829f5d1} (bbbc4926-684b-491b-9772-e0f0ffebe0af)} not associated with a fragment manager.

PostDistressDialog is a DialogFragment called from JournalEntryFragment(can be seen in map below) using the navigation component. PostDistressDialog is not an inner class of JournalEntryFragment. It is in a class of its own extending DialogFragment

Picture of my Navigation Graph

enter image description here

Function Calling NavController

public class PostDistressDialog extends DialogFragment implements ISaveDatabase {

...

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    if (getArguments()!=null) {

        ...

        // Set up the Alert Dialog
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
        alertDialog.setTitle(R.string.distressed_levels);
        alertDialog.setMessage(R.string.distressed_how_feel_post);

        // Inflate and set the layout for the dialog
        View layout = View.inflate(getActivity(), R.layout.dialog_seekbar, null);
        alertDialog.setView(layout);
        
        ....
        // Add okay button
        alertDialog.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Save post distress value in Journal Entry
                mJournalEntry.setPostDistress(mTempDistressValue);

                // Save to Journal Entry to database
                // Check if journal entry empty
                if(isJournalEntryEmpty(mJournalEntry)){
                   ...
                }
                else{
                    // Give title if empty
                    if(mJournalEntry.getTitle().isEmpty()) {
                        ....
                    // Save to database
                    new SaveDatabase(getContext(),PostDistressDialog.this).execute(mJournalEntry);
                }

                // Go to main menu
            }
        });

        return alertDialog.create();
    }

    return null;
}

...

@Override
public void databaseSavingCompleted(){
    NavHostFragment.findNavController(this).navigate(PostDistressDialogDirections.postDistressDialogToJournalListAction());
}

}

Where this is public class PostDistressDialog extends DialogFragment

Dialog in my Navigation XML File

<dialog
    android_id="@+id/postDistressDialog"
    android_name="com.dgrullon.cbtjourney.dialogs.PostDistressDialog"
    android_label="PostDistressDialog" >
    <argument
        android_name="postDistressDialogArguments"
        app_argType="com.dgrullon.cbtjourney.pojo.JournalEntries"/>
    <action
        android_id="@+id/postDistressDialog_to_journalListAction"
        app_destination="@id/journalList"
        app_popUpTo="@id/journalList"
        app_popUpToInclusive="true" />
</dialog>

Comments

Comment posted by ianhanniballake

“not associated with a fragment manager” means your Fragment is no longer attached to the FragmentManager. Where are you calling

READ  [FIXED] android - Getting a List> after a flatmap operation
Powered by Inline Related Posts

Comment posted by Rayaarito

Thanks for the quick reply @ianhanniballake. I edited my answer to show more of my

Android Tags:android, android-fragments, android-navigation, android-navigationview

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] java – Spinner Selected Item getting NullpointerExceptionError Android
[FIXED] Android devices not receiving 5% of push sent with Firebase Cloud Messaging Android
[FIXED] android – two elements in linear layout are packed to center instead of to the sides Android
[FIXED] python – FastAPI Server is not accessible from the internet Android
[FIXED] android – How to open some fragment from bottom nav bar fragment Android
[FIXED] android – How to categorize Room’s data? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme