[Android] Crash on killed app restore
See original GitHub issueHey, I am getting a crash on current version of this lib (^1.0.0-alpha.22)
2019-06-14 13:29:35.255 27866-27866/com.gelato.Optimalprint.dev E/AndroidRuntime: FATAL EXCEPTION: main Process: com.gelato.Optimalprint.dev, PID: 27866 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gelato.Optimalprint.dev/com.gelato.Optimalprint.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.Screen$ScreenFragment: calling Fragment constructor caused an exception at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.Screen$ScreenFragment: calling Fragment constructor caused an exception at android.support.v4.app.Fragment.instantiate(Fragment.java:465) at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:50) at android.support.v4.app.FragmentState.instantiate(FragmentState.java:80) at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3109) at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:158) at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:344) at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85) at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:51) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:334) at android.support.v4.app.Fragment.instantiate(Fragment.java:443) at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:50) at android.support.v4.app.FragmentState.instantiate(FragmentState.java:80) at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3109) at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:158) at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:344) at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85) at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:51) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.IllegalStateException: Screen fragments should never be restored at com.swmansion.rnscreens.Screen$ScreenFragment.<init>(Screen.java:23) at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:334) at android.support.v4.app.Fragment.instantiate(Fragment.java:443) at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:50) at android.support.v4.app.FragmentState.instantiate(FragmentState.java:80) at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3109) at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:158) at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:344) at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85) at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:51) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I guess this is happening because of this code in ScreenFragment:
public ScreenFragment() { throw new IllegalStateException("Screen fragments should never be restored"); }
It will happen to users only when the activity is killed in the background and then user tries to restore it. It is easily reproducible by setting “Don’t keep Activities” in Developer options.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:32 (3 by maintainers)
Top GitHub Comments
If anyone else has this issue, I solved it by editing my
MainActivity
so it does not try to re-open the app’s saved state:I’m facing the issue again too (using 2.4.0). I don’t believe a repo with a reproducible project is necessary in this case, as it’s very simple to reproduce and most of the steps are done in the device, rather than in the code:
On a physical device, go to Developer Options and enable the “Don’t keep activities” option; then run your project (make sure you’re calling
enableScreens()
); press the home button to put it in the background and then re-open it.The workaround I provided here still works