Question

I would like to notify ViewPagerAdapter that dataset changed in Loader.onLoadFinish callback function.

It is prevented because state of fragments might be saved.

From LoaderManager.LoaderCallbacks.onLoadFinished(Loader loader, D data) method documentation:

Note that normally an application is not allowed to commit fragment transactions while in this call, since it can happen after an activity's state is saved.`

How can I overcome this problem? How can I check in which state Activity is and commit Fragment transaction?

Was it helpful?

Solution

I've come with following solution:

Method changeCursorInOtherThread is used in onLoadFinished and onLoaderReset.

Handler is created in Activity:

private Handler mHandler = new Handler();

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mSaved = true;

}

private void changeCursorInOtherThread(final Cursor data) {
    if(!mSaved){
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                mViewPagerAdapter.changeCursor(data);
            }
        });
    }
}

OTHER TIPS

I had a similar problem. Wanted to show a DialogFragment after onLoadFinished(). As you said it's not possible to commit fragment transaction inside onLoadFinished(). But then I realized, that I could just implement LoaderCallbacks in the DialogFragment. And it works like a charm. Instead of listening for the Loader to finish its job within an Activity, I do it inside the Fragment.

You could do the same. Instead of implementing onLoadFinished() inside your activity you could just implement it inside Fragments which are placed in your ViewPager. Of course in some cases it doesn't make sense in a ViewPager, it depends on your data structure.

There's more discussion on this issue here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top