Home>

If i press any place other than the dialog, the dialog will disappear.
I want to delete the dialog only when the button of the dialog is pushed.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        DialogFragmentA dialogFragment = new DialogFragmentA ();
        dialogFragment.show (getSupportFragmentManager (), "DialogFragmentA");
    }
}
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class DialogFragmentA extends DialogFragment {
    @Override
    public Dialog onCreateDialog (Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder (getActivity ());
        // dialog title
        builder.setTitle ("Do I want to exit the app");
        // Button to exit the app
        builder.setPositiveButton ("Exit app", new DialogFragmentA.DialogButtonClickListener ());
        // I want to close the dialog only when this button is pressed
        builder.setNeutralButton ("Cancel", new DialogFragmentA.DialogButtonClickListener ());
        AlertDialog dialog = builder.create ();
        return dialog;
    }
    private class DialogButtonClickListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick (DialogInterface dialog, int which) {
            switch (which) {
                // Process to close the application
                case DialogInterface.BUTTON_POSITIVE:
                    break;
                 // Process to close the dialog
                case DialogInterface.BUTTON_NEGATIVE:
                    break;
            }
        }
    }
}