How to execute fragment transactions in a FragmentPagerAdapter in the onLoadFinish callback method?

I would like to notify ViewPagerAdapterthat the data set has been changed in the callback function Loader.onLoadFinish.

This is prevented because the state of the fragments can be saved.

From LoaderManager.LoaderCallbacks.onLoadFinished (bootloader loader, D data) :

Please note that usually the application is not allowed to complete fragment transactions during this call, as this may happen after the activity state has been saved.

How can I solve this problem? How can I check in what condition Activityand transaction is completed Fragment?

+5
source share
2 answers

I came up with the following solution:

The method is changeCursorInOtherThreadused in onLoadFinishedand onLoaderReset.

Handlercreated 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);
            }
        });
    }
}
+4
source

I had a similar problem. Required to show DialogFragmentafter onLoadFinished(). As you said, it is not possible to complete a fragment transaction inside onLoadFinished(). But then I realized that I could just implement LoaderCallbacksin DialogFragment. And it works like a charm. Instead of listening Loaderto complete my assignment in Activity, I do it inside Fragment.

. onLoadFinished() , ViewPager. , ViewPager, .

.

+1

All Articles