react-native-screens
react-native-screens copied to clipboard
Android App Crash because of this
Description
Our Android app crash and got this report
Caused by java.lang.IllegalStateException: Screen fragments should never be restored. Follow instructions from https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704067 to properly configure your main activity. at com.swmansion.rnscreens.ScreenFragment.(ScreenFragment.java:44) at com.swmansion.rnscreens.ScreenStackFragment.(ScreenStackFragment.java:75) at java.lang.reflect.Constructor.newInstance0(Constructor.java) at java.lang.reflect.Constructor.newInstance(Constructor.java:343) at androidx.fragment.app.Fragment.instantiate(Fragment.java:613) at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483) at androidx.fragment.app.FragmentStateManager.(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:313) at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:273) at com.sixdays.truckerpath.renren.view.base.RRBaseSplitScreenActivity.onCreate(RRBaseSplitScreenActivity.kt:45) at com.sixdays.truckerpath.renren.view.base.RRMainBaseSplitActionPresenterActivity.onCreate(RRMainBaseSplitActionPresenterActivity.kt:39) at com.sixdays.truckerpath.renren.view.main.RRMainActivity.onCreate(RRMainActivity.java:575) at android.app.Activity.performCreate(Activity.java:8282) at android.app.Activity.performCreate(Activity.java:8262) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3996) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4188) at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:6295) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:6183) at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:65) at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:6254) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2435) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8582) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
with these versions "react-native": "0.72.8" "react-native-screens": "2.15.0", // I try 3.28.0 and 3.14.0 not work "@react-navigation/native": "^6.1.9", "@react-navigation/native-stack": "^6.9.17",
Steps to reproduce
To reproduce it
- Invert screen, change your Android device from landscape to portrait
- change to the night mode
Snack or a link to a repository
company
Screens version
2.15.0
React Native version
0.72.8
Platforms
Android
JavaScript runtime
None
Workflow
None
Architecture
None
Build type
None
Device
None
Device model
No response
Acknowledgements
Yes
Hey! 👋
The issue doesn't seem to contain a minimal reproduction.
Could you provide a snack or a link to a GitHub repository under your username that reproduces the problem?
Hi @liujiangping, thank you for creating this issue! First, I just need to ask, have you tried to follow instructions, mentioned in the stack trace?
Hi @tboba I think this is the stack trace.
Caused by java.lang.IllegalStateException: Screen fragments should never be restored. Follow instructions from https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704067 to properly configure your main activity. at com.swmansion.rnscreens.ScreenFragment.(ScreenFragment.java:44) at com.swmansion.rnscreens.ScreenStackFragment.(ScreenStackFragment.java:75) at java.lang.reflect.Constructor.newInstance0(Constructor.java) at java.lang.reflect.Constructor.newInstance(Constructor.java:343) at androidx.fragment.app.Fragment.instantiate(Fragment.java:613) at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) at
as our Android developer(I'm frontend) told me that, our app use fragment in the main page, and we transfrom one of it into RN then use react-natvie-navigation. Then we got this App crash.
BTW: we use these code and can not change it because of some Business requirements
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ....... }
@liujiangping Yeah, I understand, but you can't call onCreate with savedInstanceState
. What Android tries to do here, is that it tries to restore the state (your app may be in the background, or somewhere else) from the Bundle
, but React Native Screens don't pack any state there. Moreover, React Native packs there its own stuff, resulting in having some unwanted stuff there 😅
I guess your app is Brownfield. In such case, if you have couple activities in your application, all you need to is you need to change (or add) that onCreate only in the class, that extends ReactActivity class (your class has signature extends ReactActivity
). You can also try to pass savedInstanceState
to the delegate, while this onCreate is being called. This way you will still have the access to the savedInstanceState
from the ReactActivity.
@liujiangping Yeah, I understand, but you can't call onCreate with
savedInstanceState
. What Android(安卓) tries to do here, is that it tries to restore the state (your app may be in the background, or somewhere else) from theBundle
, but React Native Screens don't pack any state there. Moreover, React Native packs there its own stuff, resulting in having some unwanted(多余) stuff there 😅I guess your app is Brownfield(棕色地带). In such case, if you have couple activities in your application, all you need to is you need to change (or add) that onCreate only in the class, that extends ReactActivity class (your class has signature
extends ReactActivity
). You can also try to passsavedInstanceState
to the delegate(委托), while this onCreate is being called. This way you will still have the access to thesavedInstanceState
from the ReactActivity.
Actually, we use a fragment to display react native page, and the fragment in a viewpager in main activity, we can't set the state to null, how can we solve this issue? Our application is very huge and old, we can't set the main activity extends ReactActivity.
@liujiangping Will it be possible for you to create such ReactActivity and modify your application to use it, while you're trying to display react native page? Currently, I think that nearly every RN application should have such activity. You can take a look here how the example brownfield application looks like - https://github.com/software-mansion/react-native-screens/commit/09c4e1cfa48a4c3035e598b885e2f6db0cd6e5ba
Also, I see that you're using react-native-screens 2.15.0. Please keep in mind, that currently this version is not supported and the newest version of react-native-screens is 3.29.0. If you can, please try to upgrade this library to the newest version.
@liujiangping Will it be possible for you to create such ReactActivity and modify your application to use it, while you're trying to display react native page? Currently, I think that nearly every RN application should have such activity. You can take a look here how the example brownfield application looks like - 09c4e1c
Also, I see that you're using react-native-screens 2.15.0. Please keep in mind, that currently this version is not supported and the newest version of react-native-screens is 3.29.0. If you can, please try to upgrade this library to the newest version.
Sorry, unfortunately I can't display the react native page in a ReactActivity, we must set the react native page in a fragment and the fragment is in the existed viewpager and Activity.
this is how our App looks like
@tboba I am sorry, but thats NOT a solution and never was. The bug exists since 2018, yet instead of fixing it, we should pass null? Who ever thought thats even a good idea?
And no, we cannot disable config changes, as our app has a drawer in portrait on tablets and in landscape not (more space).
We also need to pass the bundle, so that the app resumes after rotating.
It would be really great, if you could finally fix this BUG (from 2018) and not tell people, to destroy the Android life-cycle by passing null as saved state.
Hey @devjta 👋 I understand your frustration, but let's try to be polite without being passive-aggressive. We maintain free-to-use open-source libraries, and we put a lot of effort into making them as good as possible. However, our time is limited - there are only 24 hours in a day 🤷♂️. Unfortunately, we can't handle every single issue.
If you disagree with @tboba and you know how to solve this issue, we would be more than happy to assist you in creating a PR for react-native-screens so that everyone can benefit. 🎉
For priority support, you can reach out to the SWM sales department at https://swmansion.com/contact/projects 😅