Thunder Rabbit
Thunder Rabbit

Reputation: 5449

ProgressBar not updating correctly after return to Activity

MyBookshelf extends Activity and implements an Interface called by asynchronous download and unzip processes.

The asynchronous download process periodically calls onBookDownloadPercent(float) from the Interface. My Activity definition includes:

public class MyBookshelf extends Activity implements BookDownloadListener {

    private ProgressBar mProgress;

    // .......
    // .......

    @Override
    public void onBookDownloadPercent(float _percent_finished) {
        final float final_percent  = _percent_finished;
        runOnUiThread(new Runnable() {
            public void run()
            {
                DebugLog.d(TAG, "onBookDownloadPercent " + Float.toString(final_percent));
                mProgress.setProgress((int) final_percent);
            }
        });
    }
}

In LogCat, my function DebugLog.d assures me that the UI thread is calling mProgress.setProgress():

09-30 11:37:06.698: DEBUG/MyBookshelf(2069): onBookDownloadPercent 0.0014221431; thread 1
09-30 11:37:07.288: DEBUG/MyBookshelf(2069): onBookDownloadPercent 1.0004596; thread 1
09-30 11:37:07.838: DEBUG/MyBookshelf(2069): onBookDownloadPercent 2.0001705; thread 1
09-30 11:37:08.318: DEBUG/MyBookshelf(2069): onBookDownloadPercent 3.0014038; thread 1
09-30 11:37:09.098: DEBUG/MyBookshelf(2069): onBookDownloadPercent 4.0002813; thread 1
09-30 11:37:09.468: DEBUG/MyBookshelf(2069): onBookDownloadPercent 5.0003366; thread 1
09-30 11:37:09.818: DEBUG/MyBookshelf(2069): onBookDownloadPercent 6.0003924; thread 1
09-30 11:37:10.158: DEBUG/MyBookshelf(2069): onBookDownloadPercent 7.0004926; thread 1
09-30 11:37:10.588: DEBUG/MyBookshelf(2069): onBookDownloadPercent 8.000748; thread 1
09-30 11:37:10.938: DEBUG/MyBookshelf(2069): onBookDownloadPercent 9.000215; thread 1
09-30 11:37:11.368: DEBUG/MyBookshelf(2069): onBookDownloadPercent 10.00017; thread 1

I have a similar Interface for the asynchronous unzip task:

@Override
public void onBookUnzipPercent(float _percent_finished) {
    final float final_percent  = _percent_finished;
    runOnUiThread(new Runnable() {
        public void run()
        {
            DebugLog.d(TAG, "onBookUnzipPercent " + Float.toString(final_percent));
            mProgress.setProgress((int) final_percent);
        }
    });
}

First, MyBookshelf calls the asynchronous task which downloads the first book, and the progress bar updates correctly (0 to 100%).

Second, MyBookshelf calls the asynchronous task which unzips the first book, and the progress bar updates correctly (0 to 100%).

Third, MyBookshelf calls startActivity(nextIntent) to read the book and calls finish() on itself.

Fourth, I come back to MyBookshelf after reading the book:

nextIntent = new Intent(getBaseContext(), MyBookshelf.class);

The Problem occurs when I download the next book: the download occurs, and LogCat logs the updates, but the progress bar is only visible as blank gray and does not update when downloading nor when unzipping the next book. I have confirmed with LogCat that thread 1 is indeed running via the onBookDownloadPerecent Runnable

Do I need to do something interesting to mProgress when coming back to MyBookshelf so it will display when it's updated?

Upvotes: 1

Views: 397

Answers (2)

Thunder Rabbit
Thunder Rabbit

Reputation: 5449

My solution was to make mProgressBar static:

public class MyBookshelf extends Activity implements BookDownloadListener {

    static private ProgressBar mProgress;

    // .......
    // .......

    @Override
    public void onBookDownloadPercent(float _percent_finished) {
        final float final_percent  = _percent_finished;
        runOnUiThread(new Runnable() {
            public void run()
            {
                DebugLog.d(TAG, "onBookDownloadPercent " + Float.toString(final_percent));
                mProgress.setProgress((int) final_percent);
            }
        });
    }
}

Upvotes: 1

Terence Lui
Terence Lui

Reputation: 2808

WHy dont you use the finish() when going back to the myBookShelf after reading the book?

It seems everything is ok, but I have a little doubt about this line of code

nextIntent = new Intent(getBaseContext(), MyBookshelf.class);

Upvotes: 0

Related Questions