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.
Your code seems correct, but try some things and let's see if it resolves:
To give you a precise answer we need some information like:
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