1

I am using a ViewPager from the compatibility library. I get this exception quite often from the market reports.

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1242)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1253)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:535)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:519)

Any ideas what might cause it?

1
  • See this blog post for more information about why this exception is thrown. Commented Aug 20, 2013 at 22:34

2 Answers 2

8

Try commitAllowingStateLoss() instead of commit(). Read the documentation here.

Got answer from here.

Sign up to request clarification or add additional context in comments.

2 Comments

-1, this is not a good solution to this problem. All commitAllowingStateLoss() does is avoid the exception and allows the state loss to occur.
commitAllowingStateLoss it not a solution. Maybe it will work this time, but you can have problems to view a fragment sometimes.
4

You are probably calling FragmentTransaction.commit() in the wrong place. It needs to be called before state is saved.

4 Comments

yes that is true, but the hard thing is figuring out where :)
@Nicolay But if I only call commit() on Fragments onCreate() and activity is just created (onResume() still not called) can state has been saved?
this doesn't work if you are using DialogFragments. calling the show() method causes commit() to be called by the impl of DialogFragment, so you aren't able to substitute the "allowing state loss call" in that case.
Why would you want to use DialogFragments in a ViewPager?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.