Reputation: 1667
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();
}
});
Upvotes: 14
Views: 18744
Reputation: 5428
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;
}
}
Upvotes: 1
Reputation: 275
I faced the same issue. That's how I fixed it
private AlertDialog.Builder alertDialogBuilder;
private AlertDialog alertDialog;
Declare the above variables globally
alertDialogBuilder = new AlertDialog.Builder(this);
Then like above create alertDialogBuilder in the OnCreate method
private void AlertDialogMessage(){
alertDialogBuilder.setMessage("Internet disconnected!");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog = alertDialogBuilder.create();
}
We have just baked our alert dialog in this function. Call this function in the OnCreate method.
It should be called after the alertDialogBuilder creation statement. After that in your button OnClickListener put that below code.
if (!alertDialog.isShowing()){
alertDialog.show();
}
That's it. It fixed my issue. Hopefully, it will fix yours too.
Upvotes: 1
Reputation: 46
You can also use this code given below
private long mLastClickTime = 0;
public boolean isSingleClick() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return false;
}
mLastClickTime = SystemClock.elapsedRealtime();
return true;
}
if(isSingleClick){
openDialog();
}
Upvotes: 0
Reputation: 69
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");
Upvotes: 4
Reputation: 109237
You have to just check whether your Dialog
is already shown or not:
Dialog passwordDialog = new Dialog(SettingsActivity.this);
holder.butDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!passwordDialog.isShowing()) {
passwordDialog.show();
}
}
});
Update:
If this doesn't work in your case, then in your activity declare globally:
Dialog passwordDialog = null;
and on Button
's click:
holder.butDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(passwordDialog == null) {
passwordDialog = new Dialog(SettingsActivity.this);
passwordDialog.show();
}
}
});
Upvotes: 13
Reputation: 1277
try this.
Step 1. Declare dialog object as instance variable or global variable
private MediaPlayerDialog dialog;
Step 2. now put below code in you btton click.
if (dialog == null || (!dialog.isVisible())) {
dialog = new MediaPlayerDialog();
dialog.setData(songListDatas, selectPosition);
dialog.show(getSupportFragmentManager(), dialog.getClass().getName().toString());
}
Note : MediaPlayer
is my custom DialogFragment
you can change it according to your requirement.
Best of Luck
Upvotes: -1
Reputation: 12382
try like this...
Dialog passwordDialog = new Dialog(SettingsActivity.this);
holder.butDelete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(!passwordDialog.isShowing()) {
passwordDialog.show();
}
}
});
Upvotes: -2
Reputation: 625
Declared globally:
private Boolean dialogShownOnce = false;
private mDialog dia;
Where your dialog.show();
is called:
dia = new mDialog(getContext());
if (!dia.isShowing() && !dialogShownOnce) {
dia.show();
dialogShownOnce = true;
}
dia.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialogShownOnce = false;
}
});
mDialog does not have to be global, but I was calling mDialog.dismiss()
in some out-of-local-scope interfaces.
Still uses a Boolean
, but I don´t understand why it can´t be used.
Upvotes: 9
Reputation: 18670
You should probably have your dialogs managed by your activity, by overriding onCreateDialog()
method and then calling showDialog()
. That would solve your problem.
Callback for creating dialogs that are managed (saved and restored) for you by the activity. The default implementation calls through to onCreateDialog(int) for compatibility. If you are targeting HONEYCOMB or later, consider instead using a DialogFragment instead.
Example:
public class TestActivity extends Activity {
private static final int TEST_DIALOG_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDialog(TEST_DIALOG_ID);
}
@Override
protected Dialog onCreateDialog(int id) {
if(id == TEST_DIALOG_ID) {
Dialog passwordDialog = new Dialog(this);
return passwordDialog;
}
return super.onCreateDialog(id);
}
}
Upvotes: -1
Reputation: 1691
disable the button once you clicked it and enable again once you cancel the dialog. like below
holder.butDelete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
holder.butDelete.setEnabled(false);
Dialog passwordDialog = new Dialog(SettingsActivity.this);
passwordDialog.show();
}
});
if it didn't work you have to take one boolean variable and use that to show and cancel the dialog.
Upvotes: 1