I have an app in which the app flow is different from Android standard:
-
Standard: Pressing a button on activity A and showing loading on this activity while loading the data and building the views of activity B and only then show the next activity.
(Call AsyncTask on activity A, when finished send data and call activity B) -
What I need: Press the button on activity A, go to activity B, and while showing loading, update the view of Activity B behind the loading.
(Move from activity A to B and there call AsyncTask while showing loading. When AsyncTask is finished update the view while the loading is on front thus showing the view update process)
Problem: This will make the loading icon stop.. and sometimes show the unresponsive dialog (due to the long taking of the operation) and using the UI thread to change a view that is being built.
I’ve read some questions about the same matter: Android: how to update UI dynamically?
But none has provided a generic answer.
If you answer is “that’s not the recommended behaviour” i’ll agree… but these are requirements so i’ll really appreciate some help 😀
I first did this already a year ago in a project. I got this very same problem because I’ve tried to use an icon, and there were so much to be updated that the icon freezed sometimes, so I ended up using a
Handlerpaired withAsyncTask. It’s actually a very common pattern in pre-setup activities.Those are the steps:
Handlerobject. You could use something like this (a Loading dialog instead of aToast, obviously).AsyncTaskdoing your computations.publishProgress. Give flags to the code in order to tell it exactly which parts of the UI need to be build. You’ll see the UI being build behind while your loading dialog is displaying and spinning.onPostExecute.Below is a sample code that does exactly what you describe:
Here the Loading does not suffer from any stutter while the progress is spinning in the dialog. The dialog stays in the foreground until the
onPostExecute. You should dismiss the dialog in there, following the end of the task.