software-mansion / react-native-screens

Native navigation primitives for your React Native app.
MIT License
2.9k stars 498 forks source link

Android crash: "Unable to instantiate fragment com.swmansion.rnscreens.ScreenFragment" #1644

Closed valn1 closed 1 year ago

valn1 commented 1 year ago

Description

I am using react-native-image-picker and react-native-screens in my application. about half of the times i try to pick an image taken by the camera, the app crashes throwing

AndroidRuntime: 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.ScreenFragment: calling Fragment constructor caused an exception

I found this answer in another closed issue https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704067 but it does not work for my app because, i need to have the saved instance of the app being reopened, wich doesn't happen if i set it to null(the app restarts).

this bug only happens in a few devices tested positive for motorola one hyper and motorola moto g8 power and only happens sometimes

Steps to reproduce

execute launchCamera from 'react-native-image-picker' click ok(or similar) after taking a picture and the app will crash when returning to the application

Snack or a link to a repository


Screens version

3.18.2

React Native version

0.70.6

Platforms

Android

JavaScript runtime

Hermes

Workflow

React Native (without Expo)

Architecture

Paper (Old Architecture)

Build type

Release mode

Device

Real device

Device model

motorola one hyper

Acknowledgements

Yes

github-actions[bot] commented 1 year ago

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?

valn1 commented 1 year ago

StackTrace: image

valn1 commented 1 year ago

I'll try to make a repro outside of my project, but i can't guarantee that the issue will be reproducible in most devices

valn1 commented 1 year ago

I also tried to disable screens, but it didn't work, tha app just crashes in a different manner

kkafar commented 1 year ago

Hi @valn1, the steps you pointed out are described in installation instructions of the library and must be followed to assert that the library works properly.

Explanation why this is needed, and can't be really bypassed is also in discussion of #17 (direct link to comment).

I also tried to disable screens, but it didn't work, tha app just crashes in a different manner

If the app crashes with disabled screens it rather means that there are some more issues not related to this library, as disabling screens effectively leads to no library code being run.

Moreover to work with this issue I would need a reliable reproduction.

valn1 commented 1 year ago

i also thought it could be a different lib that was causing the issue instead of rn screens, but when screens are disabled, the same error happens, it just looks different to the user

and yes, overriding the onCreate method with null parameter does prevent the app from crashing, but the work that was being done is lost anyways because the app restarts

valn1 commented 1 year ago

i would like to understand better how this error happens and why the savedInstanceState is so unreliable. I never had any other problem like this before, for me it did the job very well

kkafar commented 1 year ago

Sorry for the late response!

The main reason we require these installation steps is to avoid inconsistencies between native & JS view hierarchies (and corresponding state). When OS kills your application and you save your native state, the same thing does not happen for JS state. Therefore, while restoring you can bring back native state (and even make the views look the same for a while), but you can't do the same with JS, thus it would lead to inconsistencies and bugs.

I'm gonna close the issue, as it was already discussed in #17, however feel free to ping me & continue the discussion, maybe we will come up with something.

swiggyrahulgupta5 commented 1 year ago

By adding the workaround fix mentioned, my other fragments in the code started crashing:

Fatal Exception: java.lang.IllegalStateException: Fragment no longer exists for key f#0: unique id 0b6a09fd-22ff-413b-ad75-4512fc6f6d6b at androidx.fragment.app.FragmentManager.getFragment(FragmentManager.java:960) at androidx.viewpager2.adapter.FragmentStateAdapter.restoreState(FragmentStateAdapter.java:549) at androidx.viewpager2.widget.ViewPager2.restorePendingState(ViewPager2.java:350) at androidx.viewpager2.widget.ViewPager2.dispatchRestoreInstanceState(ViewPager2.java:375) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4219) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4219) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4219) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4219) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4219) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:4219) at android.view.View.restoreHierarchyState(View.java:22723) at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2287) at android.app.Activity.onRestoreInstanceState(Activity.java:1820) at android.app.Activity.performRestoreInstanceState(Activity.java:1773) at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1431) at android.app.ActivityThread.handleStartActivity(ActivityThread.java:4207) at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2571) 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:8741) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

mrtawil commented 1 year ago

Sorry for the late response!

The main reason we require these installation steps is to avoid inconsistencies between native & JS view hierarchies (and corresponding state). When OS kills your application and you save your native state, the same thing does not happen for JS state. Therefore, while restoring you can bring back native state (and even make the views look the same for a while), but you can't do the same with JS, thus it would lead to inconsistencies and bugs.

I'm gonna close the issue, as it was already discussed in #17, however feel free to ping me & continue the discussion, maybe we will come up with something.

This fixed my problem.

kabottin0 commented 9 months ago

Hello everyone, in my project, it has happened very rarely, and we cannot replicate the crash. The react-native-screens library is at version 3.18.2, and it occurred on Android phones running version 10-11-12. In addition to the suggested solutions, do you have any other advice? thanks for collaboration

valn1 commented 9 months ago

@kabottin0 probably happens only when the Operating System kills the application in the background, try changing your phone configs to have a maximum of 1 app opened at a time, open your app and then, open a different one and finally, switch to your app, that will trigger the onCreate method which will break as the savedInstance is no longer available

alialim commented 1 month ago

yesterday i also updated to the latest React native version 74.1 and updated all packages that are related to react-native-screens. But still the same error. I thought it might be related to react-native": ‘0.73.2’, but unfortunately it was not. I'm slowly running out of ideas. Has anyone else found a solution?

kkafar commented 1 month ago

Hey @alialim, are you able to provide a reproduction of this issue?

aliamjad2 commented 3 weeks ago

I found this helpful https://stackoverflow.com/questions/57709742/unable-to-instantiate-fragment-com-swmansion-rnscreens-screen