I have an AsyncTask that shows a progressDialog whilst working (it calls runOnUiThread from within doInBackground to show the progress dialog).
Whilst its running I want to allow the use of the back button to cancel the operation; someone else has had this problem: BACK Button is not working ,while progressDialog is running
For what ever reason I can’t reply to that thread, hence having to start another?! (Another question for another day)
I had the same idea as Sandy but this code is never called whilst the progressDialog is showing, why is this? I have implemented it inside my main activity class, does the progressDialog take the foreground focus away from my class temporarily?
First, you should show your dialog from
OnPreExecute, hide it inOnPostExecute, and – if necessary – modify it by publishing progress. (see here)Now to your question:
ProgressDialog.show()can take aOnCancelListeneras an argument. You should provide one that callscancel()on the progress dialog instance.example:
where
_progressDialogis aProgressDialogmember ofYourTask.