blackHawk
blackHawk

Reputation: 6307

AlertDialog poping up twice

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);
            }
        });

    }

Upvotes: 1

Views: 3395

Answers (3)

Abhishek A Udupa
Abhishek A Udupa

Reputation: 432

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.

Upvotes: 1

Hossein Tavangar
Hossein Tavangar

Reputation: 31

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();

Upvotes: 3

blackHawk
blackHawk

Reputation: 6307

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!

Upvotes: 4

Related Questions