Ram Koti
Ram Koti

Reputation: 2211

How to delete alertDialog.builder in android programmatically

I have a sample code to show a pop-up message(alertDialog.butiler) for every 20 seconds which is kept in the scheduler. the problem is a new window pop-up is overwritten on the old pop-up for every 20 sec's, so how can I overcome this, I need to get a single pop-up and if that pop-up is alive the new pop-up window should not be generated. so, please help me.

This is my sample code:

import android.support.v7.app.AlertDialog;
import android.content.DialogInterface;


ScheduledFuture<?> s = null;
private ScheduledExecutorService scheduler = null;
scheduler=Executors.newScheduledThreadPool(1);
s=scheduler.scheduleAtFixedRate(new

Runnable() {
     @Override
     public void run () {
            System.out.println("Called the scheduler");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (showAlertDialog) {
                        if (!isFinishing()) {
                            System.out.println("Called the scheduler on ui thread");
                            AlertDialog.Builder builder = new AlertDialog.Builder(WaitForRiderAllocationActivity.this);
                            builder.setCancelable(false);
                            builder.setTitle("TRIP UPDATES");
                            builder.setMessage("All are our riders are busy, do you want to still continue?");
                            builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    finish();
                                }
                            });
                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(WaitForRiderAllocationActivity.this, "some text", Toast.LENGTH_LONG).show();
                                    Intent intent = new Intent(WaitForRiderAllocationActivity.this, GiveRideTakeRideActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                            builder.show();
                        }
                    }
                }

                );
            }
        },
        0, 20, TimeUnit.SECONDS);

Upvotes: 0

Views: 2324

Answers (2)

Suresh Kumar
Suresh Kumar

Reputation: 2034

Keep the AlertBuilder object global and inside your scheduler check if the builder object is null and create your dialog

// Global object
AlertBuilder builder; 


...
    s = scheduler.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        System.out.println("Called the scheduler");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (showAlertDialog){
                    if(!isFinishing()){
                        System.out.println("Called the scheduler on ui thread");
                        if(builder == null){
                            builder = new AlertDialog.Builder(WaitForRiderAllocationActivity.this);                         builder.setCancelable(false);
                            builder.setTitle("TRIP UPDATES");
                            builder.setMessage("All are our riders are busy, do you want to still continue?");
                            builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    finish();
                                }
                            });
                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(WaitForRiderAllocationActivity.this,"some text",Toast.LENGTH_LONG).show();
                                    Intent intent = new Intent(WaitForRiderAllocationActivity.this, GiveRideTakeRideActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                            builder.show();
                        }
                    }
                }
            });
        }
    },
    0, 20,
            TimeUnit.SECONDS);

Upvotes: 0

RadekJ
RadekJ

Reputation: 3043

Keep reference to dialog you have shown:

 if(mDialog!=null) {
      mDialog.dismiss();
      mDialog = null;
 }
 . . .
 mDialog = builder.show();

Upvotes: 2

Related Questions