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?
Its very simple just copy the below code and paste within Async task..
ProgressDialog dialog;
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 aOnCancelListener
as an argument. You should provide one that callscancel()
on the progress dialog instance.example:
where
_progressDialog
is aProgressDialog
member ofYourTask
.I just found a perfect and simplest solution to this problem. There's a method in
ProgressDialog
to set KeyListener.It's working fine with
setCancelable(false)
. I am also checking forevent.isCanceled()
, because without that I was getting two events. I've tested this on Lollipop device with and without Hardware keys.Well, I had the same issue. The simplest method that worked for me is using
progressDialog.setCancelable(true)
.. This declares whether the dialog is cancelable by hitting the back key.. Try it and let me know if it works for you or not. Good luckPlease follow this, it shows the cancel button only async and finish will call by clicking on cancel button
This can be achieved by the following code fragment:
progress is the ProgressDialog object...
This will enable the back button to close the dialog but prevent any touch input to do that...