1

I have a fragment in its on createview() method I put a condition that if this condition is true show alertdialog and any of its button clicked, dismiss dialog but on clicking dialog's button the dialog popup again, here is my condition and dialog inside it:

 if(getUser().isFirstTimeLogin() && getUser().getReceivedRequests().size() > 0 && getUser().getReceivedRequests().get(0).getStatus() == 0){

        dialog = new AlertDialog.Builder(getActivity()).create();
        LayoutInflater layoutInflater = getLayoutInflater();
        View dialogView = layoutInflater.inflate(R.layout.anonymous_login_popup, null);
        TextView title = (TextView) dialogView.findViewById(R.id.title);
        TextView description = (TextView) dialogView.findViewById(R.id.tv_anonymous_dialog_content);
        TextView okBtn = (TextView) dialogView.findViewById(R.id.okBtn);
        TextView cancelBtn = (TextView) dialogView.findViewById(R.id.cancelBtn);
        title.setText("Pending Request");
        description.setText("Your Spouse request is pending");
        okBtn.setText("Accept");
        cancelBtn.setText("Reject");
        Typeface tf = FontManager.getTypeface(getActivity(), FontManager.VARELA_ROUND);
        FontManager.setContainerTypeface(dialogView, tf);
        dialog.setView(dialogView);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.show();
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();                   acceptRejectRequest(String.valueOf(getUser().getReceivedRequests().get(0).getId()), 1);
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();                    acceptRejectRequest(String.valueOf(getUser().getReceivedRequests().get(0).getId()),     2);
            }
        });

    }
ADM
  • 20,406
  • 11
  • 52
  • 83
blackHawk
  • 6,047
  • 13
  • 57
  • 100

3 Answers3

4

Always debug your code see how code is executing, in this case the code run twice and dialogs stacked up each other so dismissing upper dialog pops dialog beneath it so seems like dialogs poping up multiple time :)

Happy coding!

blackHawk
  • 6,047
  • 13
  • 57
  • 100
  • Debugging did the trick. Apparently, I had an onTouchListener call the performClick() action under the hood, hence I had two alert dialogs invoked. – Ikhiloya Imokhai Nov 07 '19 at 08:02
3

The way I used and work perfectly for me is to define an AlertDialog instead of AlertDialog.Builder And I show the AlertDialog

This is my code:

AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.DialogTheme);
LayoutInflater inflater = LayoutInflater.from(this);
View contentView = inflater.inflate(R.layout.custom_layout , null);
builder.setView(contentView);
AlertDialog alert = builder.create();
...
//instead of builder.show()
alert.show();

And in on click just use alert.dismiss();

1

As far as displaying dialog is concerned, I always set a flag like dialogIsDisplayed, a boolean, in the utils class. Whenever there is an event which calls a dialog, I display the dialog when this flag is false. Immediately, i.e after the dialog is called, this flag is set to true.

Inside the dialog, before calling dismiss(), this flag is set to false.

This way, creation of a duplicate dialog is prevented.

Abhishek A Udupa
  • 401
  • 6
  • 13