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?

Erdal
  • 1,472
  • 3
  • 16
  • 33
  • See this [**blog post**](http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html) for more information about why this exception is thrown. – Alex Lockwood Aug 20 '13 at 22:34

2 Answers2

8

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

Got answer from here.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
parul
  • 363
  • 1
  • 6
  • 16
  • 3
    -1, this is *not* a good solution to this problem. All `commitAllowingStateLoss()` does is avoid the exception and allows the state loss to occur. – Alex Lockwood Aug 20 '13 at 22:35
  • commitAllowingStateLoss it not a solution. Maybe it will work this time, but you can have problems to view a fragment sometimes. – 1lb3r Mar 17 '15 at 15:59
4

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

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • 2
    yes that is true, but the hard thing is figuring out where :) – Erdal Sep 09 '11 at 19:12
  • 1
    @Nicolay But if I only call commit() on Fragments onCreate() and activity is just created (onResume() still not called) can state has been saved? – lujop Apr 28 '12 at 08:40
  • 1
    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. – Jeffrey Blattman Nov 05 '12 at 17:05
  • 1
    Why would you want to use DialogFragments in a ViewPager? – Nikolay Elenkov Nov 05 '12 at 17:35