Display AlertDialog extending DialogFragment or from a method in onCreateView()

Viewed 32

Hell o,

I would like to know if it's preferrable to inflate a fragment extending DialogFragment class or to display the dialog from a simple method added in onCreateView() ?

Here's a snippet for the second solution from internet (based on an activity and not fragment) :

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    positiveDialog = findViewById(R.id.btnPositiveDialog);
    negativeDialog = findViewById(R.id.btnNegativeDialog);

    positiveDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAlertDialog(R.layout.dialog_postive_layout);
        }
    });

    negativeDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAlertDialog(R.layout.dialog_negative_layout);
        }
    });

}

private void showAlertDialog(int layout){
    dialogBuilder = new AlertDialog.Builder(MainActivity.this);
    View layoutView = getLayoutInflater().inflate(layout, null);
    Button dialogButton = layoutView.findViewById(R.id.btnDialog);
    dialogBuilder.setView(layoutView);
    alertDialog = dialogBuilder.create();
    alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    alertDialog.show();
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialog.dismiss();
        }
    });
}

}

My only concern is that i use a fragment among navigationDrawer where i want to display my alertDialog (1 editText and 2 buttons), and i would like to prevent user from closing the dialog via backbutton or clicking outside the dialog.

Thank you very much for helping me out

0 Answers
Related