Hi,
at least in release builds I'm getting crashes when restoring app from background, but not always. I replaced ReactActivity with ReactFragmentActivity.
react-native: 0.57.0
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com._redacted_.app/com._redacted_.app.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:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at android.support.v4.app.Fragment.instantiate(Fragment.java:386)
at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33)
at android.support.v4.app.FragmentState.instantiate(FragmentState.java:79)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3080)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:152)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:330)
at com.facebook.react.ReactFragmentActivity.onCreate(ReactFragmentActivity.java:54)
at com._redacted_.app.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6852)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Constructor.java)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.support.v4.app.Fragment.instantiate(Fragment.java:364)
at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33)
at android.support.v4.app.FragmentState.instantiate(FragmentState.java:79)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3080)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:152)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:330)
at com.facebook.react.ReactFragmentActivity.onCreate(ReactFragmentActivity.java:54)
at com._redacted_.app.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6852)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by java.lang.IllegalStateException: Screen fragments should never be restored
at com.swmansion.rnscreens.Screen$ScreenFragment.<init>(Screen.java:19)
at java.lang.reflect.Constructor.newInstance0(Constructor.java)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.support.v4.app.Fragment.instantiate(Fragment.java:364)
at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33)
at android.support.v4.app.FragmentState.instantiate(FragmentState.java:79)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:3080)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:152)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:330)
at com.facebook.react.ReactFragmentActivity.onCreate(ReactFragmentActivity.java:54)
at com._redacted_.app.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6852)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
at com.swmansion.rnscreens.ScreenContainer.tryCommitTransaction(ScreenContainer.java:99)
at com.swmansion.rnscreens.ScreenContainer.updateIfNeeded(ScreenContainer.java:173)
at com.swmansion.rnscreens.ScreenContainer.access$000(ScreenContainer.java:21)
at com.swmansion.rnscreens.ScreenContainer$1.doFrame(ScreenContainer.java:34)
at com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame(ReactChoreographer.java:134)
at com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame(ChoreographerCompat.java:105)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:964)
at android.view.Choreographer.doCallbacks(Choreographer.java:778)
at android.view.Choreographer.doFrame(Choreographer.java:710)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6590)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
😱
As a workaround for this issue I suggest adding the following code to the main activity class (the one that extends ReactFragmentActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
}
We should perhaps make it a default behavior for the main activity class in RN core as restoring instance state won't work in RN this way anyways
Agreed. Generally speaking if you cannot save your JS memory (Redux stores, in-component state etc.), then saving only your view state could likely cause inconsistencies.
But then again in a single-activity-app, not saving the activity state means that your app is essentially restarting whenever OS kills your activity for any reason.
Does react-native-navigation attempt to deal with process death challenge, I don't know. Maybe it'd be better to let to the hosting navigation libraries deal with this. In practice they could keep saving each new screen props on into a bundle in the main activity, put this bundle into the one given in onSaveInstanceState and send the data back to as props on activity recreation through ReactRootView.startReactApplication(reactInstanceManager, moduleName, initialProps) method.
Btw my app doesn't have this problem as we have an architecture (not using this library, but I'd like to in the future) where each screen is an instance of an activity, and the props are sent through converting a ReadableMap into a Bundle and storing the Intent extras. This way we never lose the props at process death, and the props are usually sufficient to recreate the flow the user had left the app in. Not necessarily advocating for multiple activities but just explaining how we solved it.
I hit this issue too, maybe we should add the workaround in the installation instructions.
It is easy to reproduce by using the dev settings to kill background activities.
Going to add that @janicduplessis
Thanks @hey99xx for your input. The thing is that RN app is not able to restore the app state properly. So even if we were able to bring back all the views from the previous state the app won't be able to pick that up and start from there. Now when system kills the app it starts all over. In fact Android restore mechanism isn't really reliable even outside RN apps, you can try enabling developer option "don't keep activities" for a day to see it does not work in most of the apps (even some parts of the gmail app). React-native-navigation does not deal with that either, in fact it does not rely either on activities nor fragments, just have their own containers based off of ViewGroup, so behaves exactly the same way as any other RN app with regard to that.
Having each screen launch an app using startReactApplication makes your app keep the activity stack. Note that this does not guarantee you that the state is going to be restored correctly. The reasons are the same as why with many Android native apps this mechanism isn't reliable. So for example you may have some redux state that has been changed on a screen down the stack. Then when system restores the app it will open the stack of activities and load proper screens into each of the activities but there are chances some of the content on these screens may rely on the redux state that is going to be cleared (unless persisted) when the app gets killed.
I understand your reasonings and that's what I mentioned in my first paragraph too, saving only your view state is solving only half of the problem, and will cause inconsistencies as your Redux state will be entirely gone unless you find ways to persist it (I guess redux-persist could work here). I don't think we have a disagreement here.
Then when system restores the app it will open the stack of activities and load proper screens into each of the activities but there are chances some of the content on these screens may rely on the redux state that is going to be cleared (unless persisted) when the app gets killed.
This is also true, but from my experience props tend to be sufficient, and saving further state in the activity is not super necessary. In fact this approach is a bit similar to WebView.saveState(Bundle) method as it only saves the back/forward list (something like history) and not bother with serializing the DOM or JS state itself. Of course you need to take a bit of care while passing props to make sure you can regenerate a page through props alone and do not entirely rely on local Redux stores.
We have this architecture not necessarily because it's the best but because we have a hybrid app and we need to be able to have activity stacks where some are RN based and some are pure native, so we cannot use any traditional navigation libraries. Otherwise I already understand the downsides of our activity per screen solution.
As a solution to this GitHub issue I think Nevermind you've already done it.super.onCreate(null); should be added to the README file and that should be enough.
Thanks @hey99xx I think we are on the same page with regard to that
The workaround doesn't solve it for me - anyone found something else which could cause this?
facing same crash here
+1
Thanks @K-Leon @jgcmarins @joaodematejr for leaving your notes. I'd appreciate it very much if you could find a minute to include a stacktrace from the crash you are experiencing and also version of the library + RN version your app uses.
@kmagiera sure!
I share same RN version as @ukasiu, 0.57.0
And I've got same stacktrace.
react-native-screens version: 1.0.0-alpha.21
java.lang.IllegalStateException: In order to use RNScreens components your app's activity need to extend ReactFragmentActivity or ReactCompatActivity
at com.swmansion.rnscreens.ScreenContainer.<init>(ScreenContainer.java:44)
at com.swmansion.rnscreens.ScreenContainerViewManager.createViewInstance(ScreenContainerViewManager.java:21)
at com.swmansion.rnscreens.ScreenContainerViewManager.createViewInstance(ScreenContainerViewManager.java:9)
at com.facebook.react.uimanager.ViewManager.createView(ViewManager.java:42)
at com.facebook.react.uimanager.NativeViewHierarchyManager.createView(NativeViewHierarchyManager.java:260)
at com.facebook.react.uimanager.UIViewOperationQueue$CreateViewOperation.execute(UIViewOperationQueue.java:200)
at com.facebook.react.uimanager.UIViewOperationQueue$1.run(UIViewOperationQueue.java:888)
at com.facebook.react.uimanager.UIViewOperationQueue.flushPendingBatches(UIViewOperationQueue.java:1001)
at com.facebook.react.uimanager.UIViewOperationQueue.access$2400(UIViewOperationQueue.java:46)
at com.facebook.react.uimanager.UIViewOperationQueue$2.runGuarded(UIViewOperationQueue.java:959)
at com.facebook.react.bridge.GuardedRunnable.run(GuardedRunnable.java:24)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
@jgcmarins can you please try it with [email protected] then?
@K-Leon thanks for sending the stacktrace but it is missing the top of the trace (where the exception is specified) which is the most important part. Also if exception has a cause ("caused by" section) that would be an important information too
@kmagiera version 1.0.0-alpha.22 did the trick.
Thanks \o/
I also updated to rn 0.59.
Hello,
I am on rn 0.58.6 and 1.0.0-alpha.22 and just had a similar crash report
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11 (Unknown Source)
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:6501)
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:
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 com.facebook.react.ReactFragmentActivity.onCreate (ReactFragmentActivity.java:54)
at com.stocksinplay.sip.MainActivity.onCreate (MainActivity.java:27)
at android.app.Activity.performCreate (Activity.java:7026)
at android.app.Activity.performCreate (Activity.java:7017)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2731)
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)
Caused by: java.lang.IllegalStateException:
at com.swmansion.rnscreens.Screen$ScreenFragment.<init> (Screen.java:23)
ill try to upgrade to 59 later
note its the first time i see this in weeks
Here's another occurrence with the latest version of screens (alpha 22), with the full stacktrace in a release build.
Caused by: android.support.v4.app.Fragment$InstantiationException Unable to instantiate fragment com.swmansion.rnscreens.b$a: calling Fragment constructor caused an exception
Fragment.java:386 android.support.v4.app.Fragment.instantiate
FragmentContainer.java:33 android.support.v4.app.FragmentContainer.instantiate
FragmentState:79 android.support.v4.app.FragmentState.instantiate
FragmentManager.java:3080 android.support.v4.app.FragmentManagerImpl.restoreAllState
FragmentController:152 android.support.v4.app.FragmentController.restoreAllState
FragmentActivity.java:330 android.support.v4.app.FragmentActivity.onCreate
ReactFragmentActivity.java:54 com.facebook.react.ReactFragmentActivity.onCreate
MainActivity.java:15 com.[APPNAME].android.MainActivity.onCreate
Activity.java:6910 android.app.Activity.performCreate
Instrumentation.java:1123 android.app.Instrumentation.callActivityOnCreate
ActivityThread.java:2746 android.app.ActivityThread.performLaunchActivity
ActivityThread.java:2864 android.app.ActivityThread.handleLaunchActivity
ActivityThread.java:-1 android.app.ActivityThread.-wrap12
ActivityThread.java:1567 android.app.ActivityThread$H.handleMessage
Handler.java:102 android.os.Handler.dispatchMessage
Looper.java:156 android.os.Looper.loop
ActivityThread.java:6524 android.app.ActivityThread.main
Method.java:-2 java.lang.reflect.Method.invoke
ZygoteInit.java:941 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
ZygoteInit.java:831 com.android.internal.os.ZygoteInit.main
Caused by: java.lang.reflect.InvocationTargetException
Constructor.java:-2 java.lang.reflect.Constructor.newInstance0
Constructor.java:430 java.lang.reflect.Constructor.newInstance
Fragment.java:364 android.support.v4.app.Fragment.instantiate
FragmentContainer.java:33 android.support.v4.app.FragmentContainer.instantiate
FragmentState:79 android.support.v4.app.FragmentState.instantiate
FragmentManager.java:3080 android.support.v4.app.FragmentManagerImpl.restoreAllState
FragmentController:152 android.support.v4.app.FragmentController.restoreAllState
FragmentActivity.java:330 android.support.v4.app.FragmentActivity.onCreate
ReactFragmentActivity.java:54 com.facebook.react.ReactFragmentActivity.onCreate
MainActivity.java:15 com.[APPNAME].android.MainActivity.onCreate
Activity.java:6910 android.app.Activity.performCreate
Instrumentation.java:1123 android.app.Instrumentation.callActivityOnCreate
ActivityThread.java:2746 android.app.ActivityThread.performLaunchActivity
ActivityThread.java:2864 android.app.ActivityThread.handleLaunchActivity
ActivityThread.java:-1 android.app.ActivityThread.-wrap12
ActivityThread.java:1567 android.app.ActivityThread$H.handleMessage
Handler.java:102 android.os.Handler.dispatchMessage
Looper.java:156 android.os.Looper.loop
ActivityThread.java:6524 android.app.ActivityThread.main
Method.java:-2 java.lang.reflect.Method.invoke
ZygoteInit.java:941 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
ZygoteInit.java:831 com.android.internal.os.ZygoteInit.main
Caused by: java.lang.IllegalStateException Screen fragments should never be restored
Screen.java:23 com.swmansion.rnscreens.Screen$ScreenFragment.<init>
Constructor.java:-2 java.lang.reflect.Constructor.newInstance0
Constructor.java:430 java.lang.reflect.Constructor.newInstance
Fragment.java:364 android.support.v4.app.Fragment.instantiate
FragmentContainer.java:33 android.support.v4.app.FragmentContainer.instantiate
FragmentState:79 android.support.v4.app.FragmentState.instantiate
FragmentManager.java:3080 android.support.v4.app.FragmentManagerImpl.restoreAllState
FragmentController:152 android.support.v4.app.FragmentController.restoreAllState
FragmentActivity.java:330 android.support.v4.app.FragmentActivity.onCreate
ReactFragmentActivity.java:54 com.facebook.react.ReactFragmentActivity.onCreate
MainActivity.java:15 com.[APPNAME].android.MainActivity.onCreate
Activity.java:6910 android.app.Activity.performCreate
Instrumentation.java:1123 android.app.Instrumentation.callActivityOnCreate
ActivityThread.java:2746 android.app.ActivityThread.performLaunchActivity
ActivityThread.java:2864 android.app.ActivityThread.handleLaunchActivity
ActivityThread.java:-1 android.app.ActivityThread.-wrap12
ActivityThread.java:1567 android.app.ActivityThread$H.handleMessage
Handler.java:102 android.os.Handler.dispatchMessage
Looper.java:156 android.os.Looper.loop
ActivityThread.java:6524 android.app.ActivityThread.main
Method.java:-2 java.lang.reflect.Method.invoke
ZygoteInit.java:941 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
ZygoteInit.java:831 com.android.internal.os.ZygoteInit.main
The exception root is in this line.
I am now facing a crash after closing the app and opening it again.
The stack trace is like this one: https://github.com/kmagiera/react-native-screens/issues/54
@jgcmarins did this ever get resolved for you?
Sorry, can't confirm because I am not working on that app anymore. Maybe @sibelius can help with this answer.
fixed for us
@sibelius to clarify, if you set the "Don't keep activites" flag in Developer Options and background & restore your app, does it survive?
Just received this crash report
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.[APPNAME]/com.[APPNAME].MainActivity}: b.h.a.d$e: Unable to instantiate fragment com.swmansion.rnscreens.b$a: could not find Fragment constructor
at android.app.ActivityThread.performLaunchActivity + 3114(ActivityThread.java:3114)
at android.app.ActivityThread.handleLaunchActivity + 3257(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute + 78(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks + 108(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute + 68(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage + 1948(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage + 106(Handler.java:106)
at android.os.Looper.loop + 214(Looper.java:214)
at android.app.ActivityThread.main + 7050(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java:965)
Caused by b.h.a.d$e: Unable to instantiate fragment com.swmansion.rnscreens.b$a: could not find Fragment constructor
at androidx.fragment.app.Fragment.a + 111(Fragment.java:111)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java)
at androidx.fragment.app.FragmentState.a + 25(FragmentState.java:25)
at androidx.fragment.app.FragmentManagerImpl.a + 230(FragmentManagerImpl.java:230)
at androidx.fragment.app.FragmentController.a + 4(FragmentController.java:4)
at androidx.fragment.app.FragmentActivity.onCreate + 42(FragmentActivity.java:42)
at androidx.appcompat.app.AppCompatActivity.onCreate + 40(AppCompatActivity.java:40)
at com.facebook.react.ReactActivity.onCreate(ReactActivity.java)
at com.[APPNAME].MainActivity.onCreate(MainActivity.java)
at android.app.Activity.performCreate + 7327(Activity.java:7327)
at android.app.Activity.performCreate + 7318(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate + 1271(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity + 3094(ActivityThread.java:3094)
at android.app.ActivityThread.handleLaunchActivity + 3257(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute + 78(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks + 108(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute + 68(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage + 1948(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage + 106(Handler.java:106)
at android.os.Looper.loop + 214(Looper.java:214)
at android.app.ActivityThread.main + 7050(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java:965)
md5-35c6361fa51bd9d2201e3f5b0e893f1c
Caused by java.lang.NoSuchMethodException: com.swmansion.rnscreens.b$a.<init> []
at java.lang.Class.getConstructor0 + 2328(Class.java:2328)
at java.lang.Class.getConstructor + 1725(Class.java:1725)
at androidx.fragment.app.Fragment.a + 32(Fragment.java:32)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java)
at androidx.fragment.app.FragmentState.a + 25(FragmentState.java:25)
at androidx.fragment.app.FragmentManagerImpl.a + 230(FragmentManagerImpl.java:230)
at androidx.fragment.app.FragmentController.a + 4(FragmentController.java:4)
at androidx.fragment.app.FragmentActivity.onCreate + 42(FragmentActivity.java:42)
at androidx.appcompat.app.AppCompatActivity.onCreate + 40(AppCompatActivity.java:40)
at com.facebook.react.ReactActivity.onCreate(ReactActivity.java)
at com.[APPNAME].MainActivity.onCreate(MainActivity.java)
at android.app.Activity.performCreate + 7327(Activity.java:7327)
at android.app.Activity.performCreate + 7318(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate + 1271(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity + 3094(ActivityThread.java:3094)
at android.app.ActivityThread.handleLaunchActivity + 3257(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute + 78(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks + 108(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute + 68(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage + 1948(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage + 106(Handler.java:106)
at android.os.Looper.loop + 214(Looper.java:214)
at android.app.ActivityThread.main + 7050(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java:965)
md5-35c6361fa51bd9d2201e3f5b0e893f1c
"react-native": "0.60.5",
"react-native-screens": "1.0.0-alpha.23"
Galaxy S8+
Android 9
fixed for us
I was having this issue with RN 0.60.5 and react-native-screens 1.0.0-alpha22. Easily reproducible when developer option Don't keep activities is on.
Fixed the issue as @kmagiera suggested, by applying the following diff:
diff --git a/android/app/src/main/java/com/dcnetapp/MainActivity.java b/android/app/src/main/java/com/dcnetapp/MainActivity.java
index 5604708..043ae89 100644
--- a/android/app/src/main/java/com/dcnetapp/MainActivity.java
+++ b/android/app/src/main/java/com/dcnetapp/MainActivity.java
@@ -4,6 +4,7 @@ import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
+import android.os.Bundle;
public class MainActivity extends ReactActivity {
/**
@@ -24,4 +25,9 @@ public class MainActivity extends ReactActivity {
}
};
}
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(null);
+ }
}
The fix should be OK for now, but apparently it's not a good long term solution. Is there any more proper way to solve for this?
I've just got the followings.
When I move change tab & change back to previous tab.
It happened when switching to any tab which has already opened.
and the solution https://github.com/kmagiera/react-native-screens/issues/17#issuecomment-424704067
didn't help ðŸ˜
D AndroidRuntime: Shutting down VM
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.emergencyapp.debug, PID: 11743
E AndroidRuntime: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1536)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1558)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.popBackStack(FragmentManagerImpl.java:258)
E AndroidRuntime: at com.swmansion.rnscreens.ScreenStack.setupBackHandlerIfNeeded(ScreenStack.java:212)
E AndroidRuntime: at com.swmansion.rnscreens.ScreenStack.onAttachedToWindow(ScreenStack.java:80)
E AndroidRuntime: at android.view.View.dispatchAttachedToWindow(View.java:19553)
E AndroidRuntime: at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3430)
E AndroidRuntime: at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
E AndroidRuntime: at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
E AndroidRuntime: at android.view.ViewGroup.addViewInner(ViewGroup.java:5149)
E AndroidRuntime: at android.view.ViewGroup.addView(ViewGroup.java:4935)
E AndroidRuntime: at android.view.ViewGroup.addView(ViewGroup.java:4875)
E AndroidRuntime: at android.view.ViewGroup.addView(ViewGroup.java:4848)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:887)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:883)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:100)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:214)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7356)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
With following packages.
"react-native": "0.61.5",
"react-native-screens": "2.0.0-alpha.13",
"@react-navigation/bottom-tabs": "5.0.0-alpha.22",
"@react-navigation/core": "5.0.0-alpha.24",
"@react-navigation/native-stack": "5.0.0-alpha.14",
@ Android Emulator by Android Studio
Name: Pixel_3_XL_API_29
CPU/ABI: x86
Path: /Users/k/.android/avd/Pixel_3_XL_API_29.avd
Target: google_apis [Google APIs] (API level 29)
Skin: pixel_3_xl
SD Card: 512 MB
AvdId: Pixel_3_XL_API_29
PlayStore.enabled: false
avd.ini.displayname: Pixel 3 XL API 29
avd.ini.encoding: UTF-8
disk.dataPartition.size: 800M
fastboot.chosenSnapshotFile:
fastboot.forceChosenSnapshotBoot: no
fastboot.forceColdBoot: no
fastboot.forceFastBoot: yes
hw.accelerometer: yes
hw.arc: false
hw.audioInput: yes
hw.battery: yes
hw.camera.back: webcam0
hw.camera.front: emulated
hw.cpu.ncore: 2
hw.dPad: no
hw.device.hash2: MD5:9bbaa408d4a8691920c4057ef7844f35
hw.device.manufacturer: Google
hw.device.name: pixel_3_xl
hw.gps: yes
hw.gpu.enabled: yes
hw.gpu.mode: auto
hw.initialOrientation: Portrait
hw.keyboard: yes
hw.lcd.density: 560
hw.lcd.height: 2960
hw.lcd.width: 1440
hw.mainKeys: no
hw.ramSize: 1536
hw.sdCard: yes
hw.sensors.orientation: yes
hw.sensors.proximity: yes
hw.trackBall: no
image.sysdir.1: system-images/android-29/google_apis/x86/
runtime.network.latency: none
runtime.network.speed: full
showDeviceFrame: yes
skin.dynamic: yes
tag.display: Google APIs
tag.id: google_apis
vm.heapSize: 256
If I use "@react-navigation/stack" instead of "@react-navigation/native-stack",
it didn't crash.
App didn't crash if I just using StackNavigator or NativeStackNavigator.
App crashed when I switch tab using TabNavigator while using NativeStackNavigator.
<Tab.Navigator
initialRouteName="Home"
tabBarOptions={{
activeTintColor: Colors.theme,
}}
>
<Tab.Screen
name="Home"
component={HomeTab} // HomeTab is created using `NativeStackNavigator`
options={{
tabBarLabel: 'Home',
}}
/>
...
</Tab.Navigator>
I also get these crashes reported in production on Android for some users. Tried the patch https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-557939158 locally, seems to work so I will deploy that.
react-native 0.61.5
react-native-screens 1.0.0-alpha.23
react-navigation 4.0.10
react-navigation-stack 1.10.3
react-navigation-tabs 2.6.2
react-navigation/native 3.6.2
react-navigation/core 3.5.1
Line that throws in react-native-screens/android/src/main/java/com/swmansion/rnscreens/Screen.java:
public ScreenFragment() {
throw new IllegalStateException("Screen fragments should never be restored");
}
EDIT: I initially thought this appeared only after changing the launchMode of my main activity but that was wrong. These crash happened with both launchModes I've had (singleTop and singleTask).
What I get from production logs :
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3123)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3266)
at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1957)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7099)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:965)
Caused by: androidx.fragment.app.Fragment$InstantiationException:
at androidx.fragment.app.Fragment.instantiate (Fragment.java:532)
at androidx.fragment.app.FragmentContainer.instantiate (FragmentContainer.java:57)
at androidx.fragment.app.FragmentManagerImpl$6.instantiate (FragmentManagerImpl.java:2848)
at androidx.fragment.app.FragmentState.instantiate (FragmentState.java:85)
at androidx.fragment.app.FragmentManagerImpl.restoreSaveState (FragmentManagerImpl.java:2485)
at androidx.fragment.app.FragmentController.restoreSaveState (FragmentController.java:195)
at androidx.fragment.app.FragmentActivity.onCreate (FragmentActivity.java:288)
at androidx.appcompat.app.AppCompatActivity.onCreate (AppCompatActivity.java:106)
at com.facebook.react.ReactActivity.onCreate (ReactActivity.java:43)
at android.app.Activity.performCreate (Activity.java:7327)
at android.app.Activity.performCreate (Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3103)
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:514)
Caused by: java.lang.IllegalStateException:
at com.swmansion.rnscreens.Screen$ScreenFragment.<init> (Screen.java:24)
What I get on an emulator after setting developer option Don't keep activities to on, backgrounding and coming back to my app (nondeterministic but does happen) :
java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.shelvin/eu.shelvin.MainActivity}: androidx.fragment.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:3113)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3249)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)
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:1940)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:209)
at android.app.ActivityThread.main(ActivityThread.java:7046)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.Screen$ScreenFragment: calling Fragment constructor caused an exception
at androidx.fragment.app.Fragment.instantiate(Fragment.java:532)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManagerImpl$6.instantiate(FragmentManagerImpl.java:2848)
at androidx.fragment.app.FragmentState.instantiate(FragmentState.java:85)
at androidx.fragment.app.FragmentManagerImpl.restoreSaveState(FragmentManagerImpl.java:2485)
at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:195)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:288)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:43)
at android.app.Activity.performCreate(Activity.java:7658)
at android.app.Activity.performCreate(Activity.java:7647)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1295)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3088)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3249)Â
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)Â
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:1940)Â
at android.os.Handler.dispatchMessage(Handler.java:106)Â
at android.os.Looper.loop(Looper.java:209)Â
at android.app.ActivityThread.main(ActivityThread.java:7046)Â
at java.lang.reflect.Method.invoke(Native Method)Â
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)Â
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)Â
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:514)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)Â
at androidx.fragment.app.FragmentManagerImpl$6.instantiate(FragmentManagerImpl.java:2848)Â
at androidx.fragment.app.FragmentState.instantiate(FragmentState.java:85)Â
at androidx.fragment.app.FragmentManagerImpl.restoreSaveState(FragmentManagerImpl.java:2485)Â
at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:195)Â
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:288)Â
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)Â
at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:43)Â
at android.app.Activity.performCreate(Activity.java:7658)Â
at android.app.Activity.performCreate(Activity.java:7647)Â
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1295)Â
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3088)Â
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3249)Â
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)Â
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:1940)Â
at android.os.Handler.dispatchMessage(Handler.java:106)Â
at android.os.Looper.loop(Looper.java:209)Â
at android.app.ActivityThread.main(ActivityThread.java:7046)Â
at java.lang.reflect.Method.invoke(Native Method)Â
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)Â
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)Â
Caused by: java.lang.IllegalStateException: Screen fragments should never be restored
at com.swmansion.rnscreens.Screen$ScreenFragment.<init>(Screen.java:24)
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:514)Â
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)Â
at androidx.fragment.app.FragmentManagerImpl$6.instantiate(FragmentManagerImpl.java:2848)Â
at androidx.fragment.app.FragmentState.instantiate(FragmentState.java:85)Â
at androidx.fragment.app.FragmentManagerImpl.restoreSaveState(FragmentManagerImpl.java:2485)Â
at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:195)Â
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:288)Â
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)Â
at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:43)Â
at android.app.Activity.performCreate(Activity.java:7658)Â
at android.app.Activity.performCreate(Activity.java:7647)Â
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1295)Â
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3088)Â
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3249)Â
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)Â
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:1940)Â
at android.os.Handler.dispatchMessage(Handler.java:106)Â
at android.os.Looper.loop(Looper.java:209)Â
at android.app.ActivityThread.main(ActivityThread.java:7046)
...
Experiencing the same issue here. Latest version of screens and react native.
11:43:41.057 AndroidRuntime FATAL EXCEPTION: main
Process: com.app, PID: 9487
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.MainActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.ScreenStackFragment: could not find Fragment constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.ScreenStackFragment: could not find Fragment constructor
at androidx.fragment.app.Fragment.instantiate(Fragment.java:600)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:428)
at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:86)
at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2582)
at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:198)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:296)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:44)
at com.sportstack.exchange.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
... 11 more
Caused by: java.lang.NoSuchMethodException: com.swmansion.rnscreens.ScreenStackFragment.<init> []
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getConstructor(Class.java:1728)
at androidx.fragment.app.Fragment.instantiate(Fragment.java:585)
... 24 more
Same stacks as above (I dislike when people say that to me while asking to troubleshoot, but this time I swear they're the same) - fully current stable versions of everything
mike@isabela:~/work/Kullki/ksocialscore/packages/public-app (rebranding *) % cat yarn.lock |egrep "\"react-native\"|navig|screens"|grep resolved
resolved "https://registry.yarnpkg.com/@react-navigation/core/-/core-3.7.6.tgz#e0244fcdc22937825b252197f70308bbe5709c58"
resolved "https://registry.yarnpkg.com/@react-navigation/native/-/native-3.7.13.tgz#5fdf879d5af40fb5bf751b6d932dd30656c9991e"
resolved "https://registry.yarnpkg.com/react-native-screens/-/react-native-screens-2.8.0.tgz#9f989096fc5ccf248e0dfa93a74f1a64057e15f1"
resolved "https://registry.yarnpkg.com/react-navigation-drawer/-/react-navigation-drawer-2.4.13.tgz#9a5240664c1022a58a598fb326124185a6f8f0f0"
resolved "https://registry.yarnpkg.com/react-navigation-stack/-/react-navigation-stack-2.6.0.tgz#3f4f048a1c7e4ac340560f35a80fde58ccd793aa"
resolved "https://registry.yarnpkg.com/react-navigation-tabs/-/react-navigation-tabs-2.8.13.tgz#a7ec2a6b298e522370f03172bf2778f422ad4d6f"
resolved "https://registry.yarnpkg.com/react-navigation/-/react-navigation-4.3.9.tgz#3b6a7224ac2ef6388593412c33aed6eacca10cbb"
Is there anything more graceful possible then an intentional crash? I took a look at the git blame but there's no previous implementation, the first one included this. :thinking: - I'm not good enough with Fragment lifecycles yet to offer much unfortunately.
@kmagiera here you say https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704067 to change the way we call super.onCreate.
I can confirm that calling onCreate with null fixes the problem. I see that the example app also calls onCreate with null. So is this the fix for that?
If we switch super.onCreate(null) to super.onCreate(savedInstanceState); and then put the app in background and go to the android settings -> display -> font size -> change the font size and navigate to back to the app -> you will be able to reproduce the issue.
Note: App needs to be build in release mode for it to crash.
I can confirm setting up a generic "Auth Switch" with react-navigation sometimes produces this same error. Say you are on the auth stack and something in settings switches to the app stack, if you do that change with the app in the background and then try to foreground the app. It will produce the fatal error
Same issue here with react-native 0.61.5 + react-native-screens 2.8.0
Dependencies:
"dependencies": {
"@react-native-community/masked-view": "^0.1.6",
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-gesture-handler": "1.6.1",
"react-native-reanimated": "1.8.0",
"react-native-safe-area-context": "1.0.2",
"react-native-screens": "2.8.0",
"react-navigation": "4.1.1",
"react-navigation-drawer": "2.4.13",
"react-navigation-stack": "2.5.1",
"react-navigation-tabs": "2.8.13",
},
Stacktrace:
06-19 01:42:04.758 18733 18733 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.example/br.com.example.MainActivity}: androidx.fragment.app.j$b: Unable to instantiate fragment com.swmansion.rnscreens.ScreenFragment: calling Fragment constructor caused an exception
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.os.Looper.loop(Looper.java:214)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7356)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: Caused by: androidx.fragment.app.j$b: Unable to instantiate fragment com.swmansion.rnscreens.ScreenFragment: calling Fragment constructor caused an exception
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.j.a(Unknown Source:67)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.q.a(Unknown Source:0)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.z.a(Unknown Source:9)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.L.<init>(Unknown Source:10)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.C.a(Unknown Source:102)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.s.a(Unknown Source:8)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.k.onCreate(Unknown Source:17)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.appcompat.app.m.onCreate(Unknown Source:10)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at com.facebook.react.r.onCreate(Unknown Source:0)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at br.com.example.MainActivity.onCreate(Unknown Source:0)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7802)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7791)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: ... 11 more
06-19 01:42:04.758 18733 18733 E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at java.lang.reflect.Constructor.newInstance0(Native Method)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at androidx.fragment.app.j.a(Unknown Source:21)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: ... 24 more
06-19 01:42:04.758 18733 18733 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Screen fragments should never be restored
06-19 01:42:04.758 18733 18733 E AndroidRuntime: at com.swmansion.rnscreens.ScreenFragment.<init>(Unknown Source:7)
06-19 01:42:04.758 18733 18733 E AndroidRuntime: ... 27 more
same issue
I have to remove enableScreens(). After this, the bug stop here.
Is there a solution for this issue without specifying null?
This can be replicated by:
Once the app opens, savedInstanceState is not null anymore and thus throws the following error.
public ScreenFragment() {
throw new IllegalStateException("Screen fragments should never be restored");
}
@hannigand I think you may have found the one scenario where I still see this problem! Thanks for the location scenario as I do that and I still see crashes sometimes. It seems like we need to abort the entire process of handling the save instance state Bundle. There are few options:
https://stackoverflow.com/questions/6198319/android-how-to-ignore-or-disable-savedinstancestate
It seems like any of these would work:
@hannigand you seem like you're set up for testing right at this moment, I think we already have a code change as a workaround in MainActivity so focusing on those kind of solutions seems best, want to try either of the two Java changes to see if they work?
I think this issue can be closed since https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704067 is obligatory for the library to work correctly. Comment here if you have any question regarding it.
Why is this not mentioned in the readme?
A good question @DomiR 😅 if there's some place you just discovered it should go, I/we here may all be missing perspective at this point and so not have thought of it, but github can walk you through a super-quick PR if you hit the edit button at the top of whatever doc page it is
I think it would be good to mention this in README. Would you like to make a PR with a nice description? @mikehardy @DomiR? I would love to merge it.
I think this issue can be closed since #17 (comment) is obligatory for the library to work correctly. Comment here if you have any question regarding it.
@WoLewicki i have questions about it :)
What is not clear to me from the readme is if you need this onCreate "patch" only when using the complete native navigation or also if you only use enableScreens().
I have a RN app that uses React Navigation. And in both their docs as the docs here recommend calling this method for "performance and memory optimisations". So we do :) But i'm unsure if we also need to modify the onCreate if you are using react-native-screens in this way.
I hope you can help me with this! Thanks! :)
Using enableScreens() makes the Android native code use Fragments in stack, bottom-tabs, and drawer navigators, so not applying this "patch" can possibly lead to this crash.
Isn't there a proper fix for this yet? I'm experiencing the same crashes using react-native-image-picker. After taking a photo, sometimes it crashes when returning to the app. Another plugin, react-native-image-crop-picker, seems to have the same problem. The Firebase logs are the same everyone posted here (in fact, I found this post thanks to the link included in one of the errors).
I tried the proposed fix, changing super.onCreate(savedInstanceState) to super.onCreate(null), but it isn't really a fix. It just makes the app restart instead of crashing. Removing "enableScreens()" has the same effect. I still lose the entire navigation stack, and the photo I just took.
I've been searching for a solution in many githubs and stack overflow pages, but nothing worked so far. Did anyone here managed to fix this?
I think the consensus was that it is an intractable design problem because the javascript view state is not well known on Activity restore when there's a configuration change or similar, so there is nothing better / more correct to do other than assume things might be out of sync and rebuild your view from the application state you have? Thus the otherwise distasteful solution of killing the passed in state (by refusing to pass the Bundle given, and instead sending super null) is the only consistently correct thing to do
I tried to add ReactFragmentActivity but it seems it is deprecated in place of ReactActivity. Is that workaround still works? I'm afraid this can impact too much from our users.
@iagormoraes the solution works in ReactActivity too. You need to still apply this: https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704067
I’m going to share how the Navigation router solves this problem and hope you can make it work for React Navigation. The Navigation router is a 100% native navigation library for React Native so its solution might be transferable to react-native-screens.
The question is how can the navigation stack survive app restarts. Let’s say a user navigates from scene A to scene B to scene C. Then when Android restarts the app we want the stack of A → B → C to be restored. (There are a number of reasons why Android might restart an app, but the most reliable way to trigger one in an emulator is by changing the language).
React Native’s architecture helps us out here. When Android restarts our app it doesn’t restart the JavaScript environment. Take a look at how the Navigation router is created to see why this proves useful.
// This code is only ever run once at start up
const stateNavigator = new StateNavigator([
{ key: 'A' },
{ key: 'B', trackCrumbTrail: true },
{ key: 'C', trackCrumbTrail: true },
]);
const { A, B, C } = stateNavigator.states;
A.renderScene = () => <ComponentA />;
B.renderScene = () => <ComponentB />;
C.renderScene = () => <ComponentC />;
stateNavigator.navigate('A');
// The App is rendered at start up and on every restart
const App = () => (
<NavigationHandler stateNavigator={stateNavigator}>
<NavigationStack />
</NavigationHandler>
);
The stateNavigator is created outside the React component tree and it keeps track of the stack. The App component has the stateNavigator in its closure. So when Android restarts, the App component is rendered and it re-uses the stateNavigator from its closure. The Navigation router then can render the exact same stack of A → B → C because the stateNavigator survived the restart.
The only other piece of the puzzle is to ensure that all our Fragments have empty constructors so that the native side doesn’t throw an error during the restart.
Most helpful comment
As a workaround for this issue I suggest adding the following code to the main activity class (the one that extends
ReactFragmentActivity: