AlertDialog not being dismissed.

788 views Asked by At

I have created a AlertDialog fragment that is used in different activities. For some reason the alert does not get dismissed and the code in the main activity does not continue. I put in a lot of logging commands to see exactly where it stops. Below is my alert Dialog:

public class Convert_units_dialog extends DialogFragment {
final static String TAG = "TAG_convert_units";

public static Convert_units_dialog newInstance() {
    Convert_units_dialog frag = new Convert_units_dialog();
    Bundle args = new Bundle();
    args.putInt("title", R.string.change_units_dialog);
    frag.setArguments(args);
    return frag;
}


public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");

    return new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setPositiveButton(R.string.convert, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i(TAG, "inside click listener");
                    convert_units();
                    //dismiss dialog here
                    Log.i(TAG, "Start dismiss");
                    dismiss();
                    Log.i(TAG, "End dismiss");
                }
            })
            .setNegativeButton(R.string.dont_convert,null)
            .create();
}

In my activity I created a function that calls for the alertDialog.

    private void open_alert_dialog() {
    DialogFragment newFragment = Convert_units_dialog.newInstance();
    newFragment.show(getFragmentManager(), "dialog");
    Log.i(TAG,"open_alert_dialog finished");

}

When I run my code I get "end dismiss" in the log and never see "open_alert_dialog finished" so the code following this command in my activity is not being run. Is there anything obvious I am doing wrong? I have tried a lot of the suggestions I've seen, but none have worked so far.

I'm a bit new to programming in general and would appreciate any input. Thanks.

2

There are 2 answers

2
Rodrigo Direito On BEST ANSWER

Your code seems correct, but try some things and let's see if it resolves:

  • Check your imports for DialogFragment and AlertDialog, they should be the same on your Activity and the Convert_units_dialog. (If you're using appcompat-v7 library then other options for import will be shown)
  • On setPositiveButton try calling dialog.dismiss(); instead of dismiss();

To give you a precise answer we need some information like:

  • What is the implementation of convert_units();
  • Where on your Activity are you calling open_alert_dialog()

Finally, check the documentation, I believe you are already checking it, but it's worth a shot to read it again: http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog

0
ThomasK On

An update:

I think I misunderstood what was happening and have figured it out thanks to the documentation. I added a method call to doPositiveClick()

My new code looks like this:

    return new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setPositiveButton(R.string.convert, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i(TAG, "inside click listener");
                    convert_units();
                    //dismiss dialog here
                    ((Settings)getActivity()).doPositiveClick();
                }
            })
            .setNegativeButton(R.string.dont_convert,null)
            .create();

}

Then I created the doPositiveClick() in my Activity with the rest of the code that I was expecting to run after the alertDialog was dismissed.

        public void doPositiveClick() {
    // Do stuff here.
    Log.i("FragmentAlertDialog", "Positive click!");
    display_maxes();
}

Things are working as expected now.