Reputation: 70
Now on my click event I notice that sometimes that there is a chance that multiple dialogs comes out at the same time on my debugger when it lags slightly onClick.
How would we fix it, so there be a way to just make it only show 1 AlertDialog?
Code: Pretty standard.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(class1.this, class2.class);
startActivity(i);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create()
Upvotes: 0
Views: 324
Reputation: 28103
If you click the button rapidly.it queues all clicks and process them one by one. but what you want is after first click ignore rest of the clicks.
boolean isClickable = true;
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (isClickable)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener()
{
public void onClick(
DialogInterface dialog, int id)
{
Intent i = new Intent(class1.this,
class2.class);
startActivity(i);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener()
{
public void onClick(
DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
}
isClickable = false;
}
});
Upvotes: 0
Reputation: 15689
hmm try it like this:
private AlertDialog mDialog;
public void fillDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(class1.this, class2.class);
startActivity(i);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
mDialog = builder.create()
}
public void showDialog(){
if (mDialog == null) createDialog();
if (mDialog.isShowing()) return;
mDialog.show();
}
Upvotes: 0
Reputation: 8645
public void onClick(View v) {
send();
alertdialog();
alertdialog1();
}
//first
void alertdialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
/*.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});*/
AlertDialog alert = builder.create();
alert.show();
}
//////sexcond
void alertdialog1(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
/*.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});*/
AlertDialog alert = builder.create();
alert.show();
}
Upvotes: 0
Reputation: 40426
Using isShowing()
method you can check alert dialog is showing or not ..
and onClick Event create new dialog every time when you click so check if dialog is not null then create dialog if not null then check .isShowing()
so,
AlertDialog alert=null;//declare as a global ..mind that not in your onClick method
if(null=alert){
alert = new AlertDialog.Builder(this).create();
}
if(!alert.isShowing()){
//do stuff here is dialog is showing here...
}
Upvotes: 1