hBrent
hBrent

Reputation: 1716

AlertDialog Persisting on Screen Too Long

The code below is supposed to show a dialog box and then, depending on the state of the program, perform some animation (performed by the call to doseDownCourt()) when the dialog box is dismissed by clicking OK.

The problem is that the dialog box doesn't immediately go away when dismissed--it stays on the screen, with the OK button in a "pressed" state (shaded blue), while the animation is being executed and displayed. The dialog box stays on screen until the animation is complete.

I've tried to change the order in which commands are executed, and also tried to use threads to make the animation wait for the dialog box to go away before it begins, but haven't been able to get the behavior I want.

Any suggestions would be appreciated. Thanks.

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());       
    String message;
    Drawable puffle;
    Drawable dr;
    if (made == true) {
        message = "You made it!!!";
         dr = getResources().getDrawable(R.drawable.puff_happy);
    }
    else {
        message = "Aw, you missed. Better luck after your next dose!";
        dr = getResources().getDrawable(R.drawable.puff_confused);
    }
    builder.setMessage(message);        
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    puffle = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 200, 200, true));
    builder.setTitle(" ");
    builder.setIcon(puffle);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {    
            alertDialog.dismiss();
            drawAll();      //put ball and shadow back with avatar
            doseDownCourt();    //call doseDownCourt in case multiple doses have been taken before a shot for each has been attempted               
        }
    });
    alertDialog = builder.create();
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams WMLP = alertDialog.getWindow().getAttributes();
    WMLP.y = -300;   //y position of dialog box
    alertDialog.getWindow().setAttributes(WMLP);
    alertDialog.show();

Upvotes: 0

Views: 817

Answers (1)

Rolf ツ
Rolf ツ

Reputation: 8781

First of all you don't have to dismiss your alert dialog in the onClickListener, because it will be closed by default.

Second, try to run the drawAll and doseDownCourt functions in a background thread, and use a Handler to update your user interface.

Take a look at the following documentation:

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

http://developer.android.com/guide/components/processes-and-threads.html

http://developer.android.com/reference/android/os/Handler.html

So don't run process's that take lots of time on your main thread as your doing now, because onClick will be executed at the main thread.

Edit: from the docs

This code shows how to use a thread in the onClick function

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            Bitmap b = loadImageFromNetwork("http://example.com/image.png");
            mImageView.setImageBitmap(b);
        }
    }).start();
}

in your case you would change the code to something like this:

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            drawAll();
            doseDownCourt();
        }
    }).start();
}

But be careful because some things you may do in those 2 functions require to run on the UiThread, in that case you should use some kind of Handler.

Rolf

Upvotes: 1

Related Questions