I'm trying to use FragmentActivity
with ViewPager
to display dynamic list of Fragments
. There are plenty of examples on how to do a static version of it. My problem is that the list I display needs to be loaded dynamically and also can change based on user input (add/delete). I'm trying to use customized android.support.v4.content.Loader
to load set of data that I can use to build my list.
Everything goes fine in my setup until I get to the point when I want to update the adapter and issue FragmentStatePagerAdapter#notifyDataSetChanged()
call at which point this code is executed (from FragmentStatePagerAdapter
)
public void finishUpdate(View container)
{
if(mCurTransaction != null)
{
mCurTransaction.commit(); // BANG!!! The exception is thrown
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
}
The transaction commit fails with this message:
java.lang.IllegalStateException: Can not perform this action inside of onLoadFinished
because within FragmentManagerImpl
this code is executed:
private void checkStateLoss() {
if (mStateSaved) {
throw new IllegalStateException(
"Can not perform this action after onSaveInstanceState");
}
if (mNoTransactionsBecause != null) {
throw new IllegalStateException(
"Can not perform this action inside of " + mNoTransactionsBecause);
}
}
It turns out that mNoTransactionsBecause
value is not null and it is set in LoaderManagerImpl.LoaderInfo
when the result is returned back to onLoadFinished
I was looking at the various variables trying to somehow change tramsaction.commit()
to transaction.commitAllowingStateLoss()
but everything related to transaction seems to be private or at least package-protected.
Can someone give me a general idea if I can do what I need to do (and how)?
Just to note that my code works fine if instead of using Loader I run the loading ops in AsyncTask