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.
Use Dialog.setOnCancelListener to cancel your background task
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.
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With