Bitmapfun Sample: Fix bug where background image loading could hang

Background image loading is paused when the GridView is scrolling,
if the activity is destroyed during a scroll the image loading was
not unpaused causing it to remain running indefinitely. This change
makes sure the background image loading is unpaused.

Change-Id: Iad8093fc2af50fcfc1416c5d9089db0c064711cf
This commit is contained in:
Adam Koch
2013-01-28 11:54:02 -05:00
parent 690af1285c
commit 2e5555e04b
2 changed files with 14 additions and 0 deletions

View File

@@ -149,6 +149,7 @@ public class ImageGridFragment extends Fragment implements AdapterView.OnItemCli
@Override
public void onPause() {
super.onPause();
mImageFetcher.setPauseWork(false);
mImageFetcher.setExitTasksEarly(true);
mImageFetcher.flushCache();
}

View File

@@ -150,6 +150,7 @@ public abstract class ImageWorker {
public void setExitTasksEarly(boolean exitTasksEarly) {
mExitTasksEarly = exitTasksEarly;
setPauseWork(false);
}
/**
@@ -372,6 +373,18 @@ public abstract class ImageWorker {
}
}
/**
* Pause any ongoing background work. This can be used as a temporary
* measure to improve performance. For example background work could
* be paused when a ListView or GridView is being scrolled using a
* {@link android.widget.AbsListView.OnScrollListener} to keep
* scrolling smooth.
* <p>
* If work is paused, be sure setPauseWork(false) is called again
* before your fragment or activity is destroyed (for example during
* {@link android.app.Activity#onPause()}), or there is a risk the
* background thread will never finish.
*/
public void setPauseWork(boolean pauseWork) {
synchronized (mPauseWorkLock) {
mPauseWork = pauseWork;