ProgressBar not animating when inflating viewstub

I have an Activity with this structure:

FrameLayout ProgressBar ViewStub 

ViewStub inflates a fragment in a separate stream. I need to display progress during fragment loading. The problem is that the ProgressBar does not rotate while the stub is inflating (in my case about half a second: this is a heavy fragment) I tried everything: showing / hiding the view, invalid showing them in ViewSwitchers ... etc., Nothing doesn't work, as soon as the ViewStub swells up, it starts spinning, it, like u, freezes up when it swells up, but does it in another thread, it doesn't seem to improve. What should I do?

+8
android android-progressbar viewstub
source share
2 answers

The fragment must be loaded into the user interface stream, and since the user interface is occupied by the fragment, the ProgressBar does not rotate. You must split the data loading in the fragment into user interface material. I would check and verify what exactly works, and quickly run the fragment, I would use the loader to load the data, presenting the progress indicator to the user (inside the fragment). Yes, move the progress to the fragment layout and control everything from there, because I do not know when the fragment will be loaded, activity does not imply concern about this.

+4
source share

Basically, what @Luksprog says in his comment is correct, if you call viewStub.post (), it DOES NOT run the code inside the message in the background thread. All he does is runnable for the user interface thread. It can work if you do

 new Thread(new Runnable() { @Override public void run() { viewStub.inflate(); initFragment(); } }).start() 

Although, like @Luksprog also stated, it is bad practice to create views in the background thread. Therefore, perhaps the best solution is to move the .inflate () call outside (to the main thread), and then call initFragment () from the background thread and put all the heavy lifting there.

+1
source share

All Articles