In my app I am doing some intense work in AsyncTask
as suggested by Android tutorials and showing a ProgressDialog
in my main my activity:
dialog = ProgressDialog.show(MyActivity.this, "title", "text");
new MyTask().execute(request);
where then later in MyTask
I post results back to activity:
class MyTask extends AsyncTask<Request, Void, Result> {
@Override protected Result doInBackground(Request... params) {
// do some intense work here and return result
}
@Override protected void onPostExecute(Result res) {
postResult(res);
}
}
and on result posting, in main activity I hide the dialog:
protected void postResult( Result res ) {
dialog.dismiss();
// do something more here with result...
}
So everything is working fine here, but I would like to somehow to update the progress dialog to able to show the user some real progress instead just of dummy "Please wait..." message. Can I somehow access the progress dialog from MyTask.doInBackground
, where all work is done?
As I understand it is running as separate Thread, so I cannot "talk" to main activity from there and that is why I use onPostExecute
to push the result back to it. But the problem is that onPostExecute
is called only when all work is already done and I would like to update progress the dialog in the middle of doing something.
Any tips how to do this?
onProgressUpdate(Progress… ) is called on the UI thread and is used to display progress to the user while the task is running (e.g. a progress bar). You can trigger a call to 'onProgressUpdate' by calling the 'publishProgress' method.
This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.
If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes. And when it completes, the AsyncTask won't update the UI of the new Activity.
Asynchronous tasks are divided into three generic types: Params, Progress, & Result and four steps: onPreExecute, doInBackground, onProgressUpdate, & onPostExecute. The following lists the three generic types utilized in an Android AsyncTask class: Params: Parameters sent to the task upon execution.
AsyncTask has method onProgressUpdate(Integer...)
that you can call each iteration for example or each time a progress is done during doInBackground()
by calling publishProgress()
.
Refer to the docs for more details
you can update from AsyncTask's method onProgressUpdate(YOUR_PROGRESS)
that can be invoked from doInBackground
method by calling publishProgress(YOUR_PROGRESS)
the data type of YOUR_PROGRESS can be defined from AsyncTask<Int, YOUR_PROGRESS_DATA_TYPE, Long>
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