react-native-screens: [Android] Crash on killed app restore

Hey, 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.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 32 (3 by maintainers)

Most upvoted 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:

public class MainActivity extends ReactActivity {
  @Override
  protected void onCreate(Bundle savedInstance) {
    // super.onCreate(savedInstance);
    super.onCreate(null);
  }
}

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

@all In my case the app was crashing when I am rotating the screen. I had fixed this issue without compromising/disabling neither the app state nor the “enableScreens()”. I just had to update my activity in manifest file like below.

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name" 
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize" // add this line

Note: Keep in mind that this ill prevent your app from rerendering on screen orientation. But it works.

@kmagiera Still seeing this crash in react-native-screens@3.6.0

Can we re-open that issue?

The stack trace is:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3827)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4003)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2317)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8595)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
Caused by: androidx.fragment.app.Fragment$InstantiationException: 
  at androidx.fragment.app.Fragment.instantiate (Fragment.java:631)
  at androidx.fragment.app.FragmentContainer.instantiate (FragmentContainer.java:57)
  at androidx.fragment.app.FragmentManager$3.instantiate (FragmentManager.java:483)
  at androidx.fragment.app.FragmentStateManager.<init> (FragmentStateManager.java:85)
  at androidx.fragment.app.FragmentManager.restoreSaveState (FragmentManager.java:2728)
  at androidx.fragment.app.FragmentController.restoreSaveState (FragmentController.java:198)
  at androidx.fragment.app.FragmentActivity$2.onContextAvailable (FragmentActivity.java:149)
  at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable (ContextAwareHelper.java:99)
  at androidx.activity.ComponentActivity.onCreate (ComponentActivity.java:322)
  at androidx.fragment.app.FragmentActivity.onCreate (FragmentActivity.java:273)
  at com.facebook.react.ReactActivity.onCreate (ReactActivity.java:44)
  at com.acuityscheduling.client.android.MainActivity.onCreate (MainActivity.java:33)
  at android.app.Activity.performCreate (Activity.java:8207)
  at android.app.Activity.performCreate (Activity.java:8191)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3800)
Caused by: java.lang.reflect.InvocationTargetException: 
  at java.lang.reflect.Constructor.newInstance0 (Native Method)
  at java.lang.reflect.Constructor.newInstance (Constructor.java:343)
  at androidx.fragment.app.Fragment.instantiate (Fragment.java:613)
Caused by: java.lang.IllegalStateException: 
  at com.swmansion.rnscreens.ScreenFragment.<init> (ScreenFragment.kt:34)

I have the same issue after upgrade to 3.3.0. Could someone reopen this issue?

here the full stack trace:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3271)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3410)
  at android.app.ActivityThread.handleRelaunchActivityInner (ActivityThread.java:5280)
  at android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:5188)
  at android.app.servertransaction.ActivityRelaunchItem.execute (ActivityRelaunchItem.java:69)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2017)
  at android.os.Handler.dispatchMessage (Handler.java:107)
  at android.os.Looper.loop (Looper.java:214)
  at android.app.ActivityThread.main (ActivityThread.java:7397)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:492)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:935)
Caused by: androidx.fragment.app.Fragment$InstantiationException: 
  at androidx.fragment.app.Fragment.instantiate (Fragment.java:566)
  at androidx.fragment.app.FragmentContainer.instantiate (FragmentContainer.java:57)
  at androidx.fragment.app.FragmentManager$3.instantiate (FragmentManager.java:390)
  at androidx.fragment.app.FragmentStateManager.<init> (FragmentStateManager.java:74)
  at androidx.fragment.app.FragmentManager.restoreSaveState (FragmentManager.java:2454)
  at androidx.fragment.app.FragmentController.restoreSaveState (FragmentController.java:196)
  at androidx.fragment.app.FragmentActivity.onCreate (FragmentActivity.java:287)
  at androidx.appcompat.app.AppCompatActivity.onCreate (AppCompatActivity.java:106)
  at com.facebook.react.ReactActivity.onCreate (ReactActivity.java:44)
  at android.app.Activity.performCreate (Activity.java:7825)
  at android.app.Activity.performCreate (Activity.java:7814)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1307)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3246)
Caused by: java.lang.reflect.InvocationTargetException: 
  at java.lang.reflect.Constructor.newInstance0 (Native Method)
  at java.lang.reflect.Constructor.newInstance (Constructor.java:343)
  at androidx.fragment.app.Fragment.instantiate (Fragment.java:548)
Caused by: java.lang.IllegalStateException: 
  at com.swmansion.rnscreens.ScreenFragment.<init> (ScreenFragment.java:46)

I always used “react-navigation” without this lib, I was just experimenting.

The only difference you will have would be in apps that open a very large stack of screens.

In the case of these apps you will have a RAM savings using “react-native-screens”.

Similar problem here reported by the Google Play Store:

Samsung Galaxy J7 Prime (on7xelte), Android 8.1

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2974)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3059)
  at android.app.ActivityThread.-wrap11 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1724)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:7000)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:441)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)
Caused by: androidx.fragment.app.Fragment$InstantiationException: 
  at androidx.fragment.app.Fragment.instantiate (Fragment.java:465)
  at androidx.fragment.app.FragmentContainer.instantiate (FragmentContainer.java:50)
  at androidx.fragment.app.FragmentState.instantiate (FragmentState.java:80)
  at androidx.fragment.app.FragmentManagerImpl.restoreAllState (FragmentManager.java:3109)
  at androidx.fragment.app.FragmentController.restoreAllState (FragmentController.java:158)
  at androidx.fragment.app.FragmentActivity.onCreate (FragmentActivity.java:344)
  at androidx.appcompat.app.AppCompatActivity.onCreate (AppCompatActivity.java:85)
  at com.facebook.react.ReactActivity.onCreate (ReactActivity.java:51)
  at app.klist.mobileapp.MainActivity.onCreate (MainActivity.java:17)
  at android.app.Activity.performCreate (Activity.java:7258)
  at android.app.Activity.performCreate (Activity.java:7249)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1222)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2927)
Caused by: java.lang.reflect.InvocationTargetException: 
  at java.lang.reflect.Constructor.newInstance0 (Native Method)
  at java.lang.reflect.Constructor.newInstance (Constructor.java:334)
  at androidx.fragment.app.Fragment.instantiate (Fragment.java:443)
Caused by: java.lang.IllegalStateException: 
  at com.swmansion.rnscreens.Screen$ScreenFragment.<init> (Screen.java:23)

Hi, did anyone find a solution to this problem? I am still having this problem in production.

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:

public class MainActivity extends ReactActivity {
  @Override
  protected void onCreate(Bundle savedInstance) {
    // super.onCreate(savedInstance);
    super.onCreate(null);
  }
}

UPDATE I may have jumped the gun here… was debugging with the Don't keep activities developer option enabled and that seems to be the cause of the issue I described

As implied, this disables loading the saved application state when re-opening the app after backgrounding it. Be warned that this can break application functionality. This includes the navigation stack which will now be wiped-out. In our case, this has an unacceptable side-effect of not being able to use the document picker to upload pictures and other media as it would return them to the incorrect state after picking media.

This fix resolves the crash but as the activity is crashed in background, the app looses its state and gets stuck at the first screen. Navigation to further screens is blocked with this fix.

Hi, I was playing around this issue …and by chance I happened to change my laptop. We required to install react-native-screens ONLY to suffice its peer dependency for react-navigation. Previously when I had this issue reproducible in old machine, I had node version of ‘14.8.0’, where I had to manually install react-native-screens by keeping its entry in package.json. But after changing my machine, when we tested using the latest node version i.e. ‘16.5.0’ and we noticed that this version of node automatically installs all peer dependencies without having need to put them manually in package.json. Apparently this crash is fixed after moving to latest node version along with the change mentioned in above comment(sending null to onCreate). So as a fix, we removed react-native-screens from package.json and updated node to the latest version. So now, even with setting ‘Don’t keep Activities’…This crash is not reproducible for us. We are targeting to verify this on production in our next release though.

Hi, This solution has worked for us and there is no related crash event anymore on production as well.

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:

public class MainActivity extends ReactActivity {
  @Override
  protected void onCreate(Bundle savedInstance) {
    // super.onCreate(savedInstance);
    super.onCreate(null);
  }
}

UPDATE I may have jumped the gun here… was debugging with the Don't keep activities developer option enabled and that seems to be the cause of the issue I described

As implied, this disables loading the saved application state when re-opening the app after backgrounding it. Be warned that this can break application functionality. This includes the navigation stack which will now be wiped-out. In our case, this has an unacceptable side-effect of not being able to use the document picker to upload pictures and other media as it would return them to the incorrect state after picking media.

This fix resolves the crash but as the activity is crashed in background, the app looses its state and gets stuck at the first screen. Navigation to further screens is blocked with this fix.