Guerilla Coder
Guerilla Coder

Reputation: 7

Custom AlertDialog disappears on back button press

Hi guys I have a custom alert dialog I created. In the builder I set cancelable to false yet it still disappears when I press the back button, any ideas?

This is the code for the dialog:

public final class HemisphereDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View customTitle = inflater.inflate(R.layout.hemisphere_dialog_custom_title, null);
    builder.setCustomTitle(customTitle);
    String[] entries = new String[2];
    entries[0] = getResources().getString(R.string.northern_hemisphere);
    entries[1] = getResources().getString(R.string.southern_hemisphere);
    builder.setItems(entries, new DialogInterface.OnClickListener() {
        //The 'which' argument contains the index position of the selected item
        public void onClick(DialogInterface dialog, int which) {
            if( which == 0 ) {
                GlobalVariables.getShared().setIsInNorthernHemisphere(true);
            } else if( which == 1 ) {
                GlobalVariables.getShared().setIsInNorthernHemisphere(false);
            }

            ToolbarActivity.outfitsFragment.hemisphereSelected();
            GlobalVariables.getShared().setHasAskedForHemisphere(true);
        }
    });
    builder.setCancelable(false);

    //Create the AlertDialog object and return it
    return builder.create();
}

And this is how it's displayed:

new HemisphereDialogFragment().show(getSupportFragmentManager(), "hemisphereDialog");

Another small side question, is there a way to change the text size for the items in the dialog?

Upvotes: 0

Views: 238

Answers (2)

Ahmed Hegazy
Ahmed Hegazy

Reputation: 12605

You should call setCancellable(false) in the Dialog fragment itself not the AlertDialog.Builder.

Upvotes: 1

Jono
Jono

Reputation: 199

You set your alert dialog cancelable to false, but your fragment is still set to cancelable, you need to add setCancelable(false) to your fragment as well.

Upvotes: 2

Related Questions