15

I've created a fragment tab widget following fragment compatibility package sample, I change fragment in onTabChanged but I have some market reports that spots IllegalStateException after onSaveInstanceState, this is caused by asking a commit to fragment manager after calling onSaveInstanceState(), but how can I receive a performClick after onSaveInstanceState is called?

the stack trace:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1314)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1325)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:548)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:532)
at my.app.NewsTab.onTabChanged(NewsTab.java:144)
at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:378)
at android.widget.TabHost.setCurrentTab(TabHost.java:363)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:149)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:559)
at android.view.View.performClick(View.java:3122)
at android.view.View$PerformClick.run(View.java:12012)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4126)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)

looks like this was somehow discussed (by searching the error), but for me it's only a side effect, a bug in support? I've also read about a different application lifecycle for honeycomb+, but perform click after saving instance looks unrealable behavior to me

should I use commitAllowingStateLoss() or mTabHost.setOnClickListener(null) in onSaveInstanceState?

unfortunately I'm unable to reproduce, I only see rare reports on developer console

the activity has android:configChanges="orientation|screenLayout"

I've tested the lifecycle also with "always finish"

the tab fragment switcher is inside a ViewPager but I doubt can be related

sherpya
  • 4,890
  • 2
  • 34
  • 50
  • 1
    This is happening to us as well, did you find out why it happens? – alexanderblom Sep 28 '12 at 14:39
  • FWIW, a link to my similar issue that I mentioned below: http://stackoverflow.com/q/19696501/642160 -- which might at least give you some insight into repro steps. – benkc Oct 31 '13 at 00:46

3 Answers3

0

It seems to be the bug in Android from version 4.4 to version 5. See this answer for more details.

fdermishin
  • 3,519
  • 3
  • 24
  • 45
-1

The problem is that you are attempting to commit a FragmentTransaction after the Activity's onSaveInstanceState() method has been called. This is not allowed for the reasons described in this blog post.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • sure, but my app is not supposed to handle clicks after `onSaveInstanceState()` – sherpya Aug 21 '13 at 07:06
  • My guess is that the problem is not happening in response to user clicks. It is more likely happening on config changes, when the user clicks the home button and the activity is stopped and possibly killed by the system, etc. Do you know specifically when the exception is thrown? Are you able to reproduce the error on your own? – Alex Lockwood Aug 21 '13 at 14:45
  • I was unable to reproduce directly, I've only got reports on the developer console (the app is on the market), the activity uses the sample "tabs + swipe", but evolved a bit in a way that may not be 100% correct, but whatever I've done a click event in that state looks like a bug – sherpya Aug 21 '13 at 15:30
  • `onSaveInstanceState()` is called immediately before the `Activity` has gone into the background. In other words, when `onSaveInstanceState()` is called, the user interface will be hidden and therefore users shouldn't be able to click things in the first place. That's why I think that the problem has to do with configuration changes and/or something tricky in the `Activity` lifecycle which you haven't accounted for... I don't think it's possible for something like this to happen in response to a user click event. – Alex Lockwood Aug 21 '13 at 15:39
  • 10
    I'm still looking for the Right Answer, but I can confirm that it's very possible to get an onClick well after onSaveInstanceState. I have a tab activity, hosting fragment activities, some of which have buttons or lists that push new fragments in their onClick/onItemClick. If you press-and-hold one of these buttons, then tap a different tab, you'll get the onClick well after the onSaveInstanceState -- 1.05s later, in the test I just ran. – benkc Oct 31 '13 at 00:12
-1

onSaveInstanceState() is called after the Activity is gone from the foreground. You can't do anything after saving the state. Please, if you can, share us your code to check it. You have to make the changes before calling onSaveInstanceState() method.

Ahmed Awad
  • 1,787
  • 3
  • 16
  • 22