When using a AsyncTaskLoader how would you update a progressbar showing the status as it is being updated? Normally you wait for the callback to remove when done, but how to do running updates? Would you let the main thread (ui) poll the data as it is being set or something?
Edit: I'm talking about AsyncTaskLoader, look at the loader part. Here is link to class: http://developer.android.com/reference/android/content/AsyncTaskLoader.html
I want to use it because its the future :), I know how to do this with AsyncTask.
You can use handler, i think it will be lighter for system than intent
Set mHandler to constructor of AsyncTaskLoader and from loadInBackground you can update progress
Going off @garmax's answer, I found a site that showed how to combine
AsyncTaskLoader
s with Fragments on: http://habrahabr.ru/post/131560/It's in Russian, but I might post my implementation of it later.
EDIT: Here's a link to the ZIP containing that implementation: http://www.2shared.com/file/VW68yhZ1/SampleTaskProgressDialogFragme.html
I'm using this with my
AsyncTaskLoader
, inside ofloadInBackground
However, this doesn't work with an configuration change (like orientation change). I'm not convinced
AsyncTaskLoader
is the best to use if you need to update the UI, but it works best when handling configuration changes. I don't know why they created both anAsyncTaskLoader
and anAsyncTask
each with their own tradeoffs. Just frustrates and confuses developers. On top of thatAsyncTaskLoader
has very little documentation.I broadcast an Intent to the Activity (and it's received by a BroadcastReceiver). I'm not very happy with this solution but it works. The AsynTask publishProgress is really easier to use. Did you find some other solution ?
You can do that with loaders, but you need to keep and update a WeakReference on your Activity :
I just had this problem. I used a static AtomicInteger in my activity to store the progress. The loader updates it via a callback and the activity polls it and displays the progress.
In the loader callback
onLoadFinished
I hide my progress panel, which causes the polling loop to exit.Usually I'd avoid static state, but I think overall this is simpler than the alternatives. In my case, I have a different layout in landscape, so I'm happier leaving the orientation changes behaving as normal.