Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog open twice on fast click of button

I have Button, which on clicking, displays a Dialog. Everything works like a charm, but if I double click the button or click the button fast, the Dialog opens two or three times. I have to click the back button twice or thrice to dismiss the Dialog.

I have searched for related questions on SO, but most of the answers suggest disabling the button or to use a variable and setting it to true and false, which is not my requirement.

If anyone knows how to solve this problem, please help me.

Code I have used

// Delete item on click of delete button
holder.butDelete.setOnClickListener(new OnClickListener() {         
@Override
    public void onClick(View v) {
        Dialog passwordDialog = new Dialog(SettingsActivity.this);      
        passwordDialog.show();
    }
});
like image 675
Rahul Avatar asked Sep 11 '12 12:09

Rahul


2 Answers

Put this code where you want to show dialog, it checks exist dialog base on its tag name.

FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prevFragment = getFragmentManager().findFragmentByTag("dialog");
if (prevFragment != null) {
    return;
}

MyDialog dialog = MyDialog.newInstance();
dialog.show(ft, "dialog");
like image 166
Huy Tran Avatar answered Oct 28 '22 01:10

Huy Tran


When calling the show() method it takes a little while to isShowing() returns the true.
At first, it returns false because the dialog entrance animation doesn't finish yet. My solution is about building a new implementation of the dialog class.

public class ObedientDialog extends Dialog{

    private boolean showing;

    public ObedientDialog(Context context) {

        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                showing=false;
            }});

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                showing=false;
            }});
    }

    @Override
    public void show() {
        if (!showing) super.show();
        showing=true;
    }

    public boolean isShowing(){
        return showing;
    }
}
like image 23
ucMedia Avatar answered Oct 28 '22 02:10

ucMedia