java.lang.IllegalStateException crash

Hi,

The following crash report appears at least twice a week in my google play console crash reports:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1280) at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:451) at android.app.Activity.onBackPressed(Activity.java:2166) at android.app.Activity.onKeyUp(Activity.java:2144) at com.ansca.corona.CoronaActivity.onKeyUp(CoronaActivity.java:1987) at android.view.KeyEvent.dispatch(KeyEvent.java:2633) at android.app.Activity.dispatchKeyEvent(Activity.java:2374) at com.ansca.corona.input.RaiseKeyEventTask$1.run(RaiseKeyEventTask.java:87) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4830) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) at dalvik.system.NativeStart.main(Native Method)  

I understand from the above report that the crash is related to pressing the back button which i handle in my app as follows:

backBtnPressed = function(event)     local phase = event.phase     local keyName = event.keyName     if ( "back" == keyName and phase == "up" ) then         storyboard.gotoScene( previousScene, {effect = "slideRight", time = 1000, params = catg}  )         return true     end end  

In enterScene, i have the following code:  

  --listen to back button     if \_g.isAndroid then         Runtime:addEventListener( "key", backBtnPressed)     end  

    

In exitScene, i have the following code:    

 if \_g.isAndroid then         Runtime:removeEventListener( "key", backBtnPressed)     end  

Here is my problem: I have never encountered the crash during testing. And I can’t reproduce the crash. All the info i have is from the developer console crash report.

Also,

1- despite the app being updated 5 times during the past 5 months, the crash appears in all versions (different corona builds).

2- it appears in a variety of devices.

3- it only appears in android versions >= 4.0.3 (4.0.3, 4.0.4, 4.1, 4.2)

In another thread, someone suggested that the error could be caused by the facebook plugin because corona only uses the fragmentManager in that plugin. However, my app doesn’t use facebook (nor any other social plugin).

Anyone else having the same issue? Does anyone have a solution for this problem? Could someone please at least point me in the right direction?

Thanks,

Luay

It looks to me like the app or the system is trying to close the activity.

This is weird as you have return true in there.

I would move the Runtime:addEventListener( “key”, backBtnPressed) line to your main.lua as well as the function to handle it. 

Also, you do need to return true like you are, but you also need to return false at the end of the function or Android will get confused when other key’s are pressed as to if they are handled or not.

Rob

Thanks @Rob. I have made the necessary adjustments as you suggested. Hopefully the problem will be sorted out. I’ve pushed an update, so we’ll see. Thanks for your help. 

@mazrabul, you will also get this error in google if they exit the app with the back button while you are in the process of doing something like onEveryFrame or in the middle of a transition if the code is not handled correctly I had it quite a bit on my slot machines when the reals were spinning in the frame event but after fixing my code i think i have gotten 0 out of 1k downloads a day since the update (going on 10 days now).

It looks to me like the app or the system is trying to close the activity.

This is weird as you have return true in there.

I would move the Runtime:addEventListener( “key”, backBtnPressed) line to your main.lua as well as the function to handle it. 

Also, you do need to return true like you are, but you also need to return false at the end of the function or Android will get confused when other key’s are pressed as to if they are handled or not.

Rob

Thanks @Rob. I have made the necessary adjustments as you suggested. Hopefully the problem will be sorted out. I’ve pushed an update, so we’ll see. Thanks for your help. 

@mazrabul, you will also get this error in google if they exit the app with the back button while you are in the process of doing something like onEveryFrame or in the middle of a transition if the code is not handled correctly I had it quite a bit on my slot machines when the reals were spinning in the frame event but after fixing my code i think i have gotten 0 out of 1k downloads a day since the update (going on 10 days now).