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();
}
});
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");
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With