dismissing Progress Dialog
Asked Answered
L

4

9

I have a situation where I am loading a bunch of images. During this process, I am trying to show a progress Dialog until the images get loaded fully. I have overrided the onBackPressed() method, such that when the user presses the back button, the activity will be finished.

But if I press the back button when the progress dialog is being displayed, the back key event was not called. So I tried providing progressDialog.setCancelable(true). So this now allows me to dismiss the progress Dialog, but my back key event is not being called anyway and so my activity loads images in the background.

So how do I make both the progressDialog and activity to be stopped when the user presses the back key.

Logger answered 6/6, 2011 at 9:31 Comment(0)
C
7

Use Dialog.setOnCancelListener to cancel your background task

Congreve answered 6/6, 2011 at 9:34 Comment(2)
Please try to be more specific next time.Logger
@MarvinLabs :how to use that??Arthropod
L
7

Ok. I have found out a solution at last.

progressDialog.setCancelable(true);
    progressDialog.setOnCancelListener(new OnCancelListener() {

        public void onCancel(DialogInterface dialog) {

        Log.i("inside on cancel","Cancel Called");  
        finish(); //If you want to finish the activity.
        }
    });
Logger answered 6/6, 2011 at 10:42 Comment(0)
T
3

Load your images in a different thread. When the user indicates they want to cancel the load, set a flag that is checked in the loading thread's processing loop. You can also call the loading thread's interrupt() method to handle those cases where the thread is stuck in a wait() or sleep() or something.

Terrieterrier answered 9/6, 2011 at 19:53 Comment(0)
L
1

Use onKeyDown() method, however you'd have to check whether the dialog is showing, whether the button being pressed is the 'back' button, and you should also make a call to super.onKeyDown() to make sure that the default method is also executed.

 public boolean onKeyDown(int keyCode, KeyEvent event) 

{

if (keyCode == KeyEvent.KEYCODE_BACK && progressDialog.isShowing())
{
    // DO WHATEVER YOU WANT
}

// Call super code so we dont limit default interaction
super.onKeyDown(keyCode, event);

return true;
}

Can you paste the code for correct answer?

Lizethlizette answered 6/6, 2011 at 10:16 Comment(1)
where is ur answer? are you able to stop the background service?Paperback

© 2022 - 2024 — McMap. All rights reserved.