0

I've noticed that if my Activity restarts either due to a crash or due to inactivity my ProgressBar behaves weirdly. Normally my app shows a horizontal progress bar with a max and increments the counter from 0 to max as RSS feeds are loaded. However when the activity is restarted the first increment of the progress immediately takes the progress from 0 to max and stays there until the whole operation is complete.

Here is the relevant section of code:

mProgressBarText.setVisibility(View.VISIBLE);
mProgressBarProgressText.setVisibility(View.VISIBLE);
mProgressBarHorizontal.setVisibility(View.VISIBLE);

mProgressBarHorizontal.setProgress(0);
mProgressBarHorizontal.setMax(feedUrls.length);
mProgressBarProgressText.setText(mProgressBarHorizontal.getProgress() + "/" + feedUrls.length);

FeedUtils.loadFeeds(mContext, feedUrls, new FeedLoader.ProcessingCallback() {
    @Override
    public void onSuccess() {
        displayFeed(null, false, true);
    }

    @Override
    public void onIncrement() {
        mProgressBarHorizontal.setProgress(mProgressBarHorizontal.getProgress() + 1);
        mProgressBarProgressText.setText(mProgressBarHorizontal.getProgress() + "/" + feedUrls.length);
    }

    @Override
    public void onFailure() {
        displayFeed(null, false, true);
    }
});

loadFeeds() spins up Async tasks on a thread pool and uses a CountDownLatch to track thread task completion and calls either onSuccess or onFailure when complete. onIncrement is called in the postExecute of each Async task. I have checked that the incrementation code is performed on the UI thread and have tried forcing it via runOnUIThread without luck.

I've seen a few posts about bugs with the Android ProgressBar and have tried what they suggested without success:

Any ideas what I might be doing wrong here?

Community
  • 1
  • 1
alexgophermix
  • 4,189
  • 5
  • 32
  • 59
  • I would assume that when restarting, the state of the views is resetted to what it was, including the progress of the progressbar. – njzk2 Nov 09 '16 at 19:12
  • @njzk2 I thought so too but I added `mProgressBarHorizontal.setProgress(0);` and `mProgressBarHorizontal.setMax(feedUrls.length);` at the start of every load operation just to be safe but it still doesn't seem to work. For whatever reason after the initial set `mProgressBarHorizontal.setProgress(0);` the progress is correctly 0. But when I do `mProgressBarHorizontal.setProgress(mProgressBarHorizontal.getProgress() + 1);` within `onIncrement()` it immediately jumps to the max – alexgophermix Nov 09 '16 at 19:15
  • what is the value of `mProgressBarHorizontal.ge‌​tProgress()` in `onIncrement`? – njzk2 Nov 09 '16 at 19:54
  • @njzk2 I added `Log.d("TESTING", mProgressBarHorizontal.getProgress()+"");` as the first line of `onIncrement()`. When the app starts or a refresh is triggered manually this goes from 0-100 as expected (there are 101 elements in this example and the log is placed before the increment). However when the app restarts from a crash, the first time `onIncrement()` is fired it's already 101 – alexgophermix Nov 09 '16 at 20:25
  • so it really looks like the view state is being restored. This happens after `onCreate`, so if you change the state in this method, that won't do anything. – njzk2 Nov 09 '16 at 21:52
  • 1
    I would recommend that you refactor `onIncrement` so that this method gives you the progress and the total directly, instead of keeping track of that outside. – njzk2 Nov 09 '16 at 21:52
  • @njzk2 you're the best, this put me on the right track. It looks like this restoration results in a progress bar that is already full (because i left it in a full state before the crash). The `onIncrement` refactor almost worked, but for a brief moment the bar is full before the first increment is fired which sets it to 1/101. My workaround is just to always flush the progress bar upon completion. Do you want to answer this question with what you suggested and your explanation of why it wasn't working? If you have any progress bar best practices that would be great too! – alexgophermix Nov 09 '16 at 22:34

0 Answers0