Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
valn1 opened this issue Nov 21, 2022 · 24 comments
Assignees
Labels
Missing repro This issue need minimum repro scenario Platform: Android This issue is specific to Android

Comments

@valn1
Copy link

valn1 commented Nov 21, 2022

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
#17 (comment)
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 github-actions bot added the Missing repro This issue need minimum repro scenario label Nov 21, 2022
@github-actions
Copy link

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?

@github-actions github-actions bot added the Platform: Android This issue is specific to Android label Nov 21, 2022
@valn1
Copy link
Author

valn1 commented Nov 21, 2022

StackTrace:
image

@valn1
Copy link
Author

valn1 commented Nov 21, 2022

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
Copy link
Author

valn1 commented Nov 21, 2022

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

@kkafar
Copy link
Member

kkafar commented Nov 24, 2022

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.

@kkafar kkafar added the Close when stale This issue is going to be closed when there is no activity for a while label Nov 24, 2022
@kkafar kkafar self-assigned this Nov 24, 2022
@valn1
Copy link
Author

valn1 commented Nov 24, 2022

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

@github-actions github-actions bot removed the Close when stale This issue is going to be closed when there is no activity for a while label Nov 24, 2022
@valn1
Copy link
Author

valn1 commented Nov 24, 2022

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
Copy link
Member

kkafar commented Jan 17, 2023

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.

@kkafar kkafar closed this as completed Jan 17, 2023
@swiggyrahulgupta5
Copy link

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
Copy link

mrtawil commented Feb 10, 2023

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
Copy link

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
Copy link
Author

valn1 commented Sep 18, 2023

@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
Copy link

alialim commented May 23, 2024

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
Copy link
Member

kkafar commented May 25, 2024

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

@aliamjad2
Copy link

@FazilMuhammed
Copy link

I am using latest React Native Version 0.74.2. I have a mainactivity.kt inside android/app/src/main/java

how to fix this issue please help anyone

@phil-hudson
Copy link

seeing thew same on React Native Version 0.74.3

@valn1
Copy link
Author

valn1 commented Jul 12, 2024

@phil-hudson @FazilMuhammed unfortunatelly i haven't been able to fix this issue myself. what i did was work around it, you gotta find out what action makes your app crash, then make the app do something else instead.
for example: i had this app that would cash on some devices sometimes when the user were to select files from their gallery/camera using the lib react-native-image-picker.
I stopped bothering to solve that and instead made a config in the app that the user could opt into to use a camera component i made that has also gallery inside.

and so i got around the crash by literally avoiding it.

my solution was very convoluted and demanded a whole new module to my app, but you could probably just rewrite the bit that breaks and do it differently so it does not crash anymore.

@alainib
Copy link

alainib commented Jul 23, 2024

i have this error but only when user open my react native app, go to android settings, change font size and come back on app

@jlmosconi
Copy link

I am using latest React Native Version 0.74.2. I have a mainactivity.kt inside android/app/src/main/java

how to fix this issue please help anyone

Have you tried adding this to your MainActivity.kt file?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(null)
}

@muxbert
Copy link

muxbert commented Jul 27, 2024

In my case, this issue happens with every trigger to Alert.alert(), so I just replaced it with Custom Alert components, also I used react-native-flash-message, however, this is still a workaround, I hope it helps someone.

@ukcasso
Copy link

ukcasso commented Aug 8, 2024

@muxbert @FazilMuhammed
It seems to be related to react-native-reanimated. Please check the link. #2287

@kkafar

This comment was marked as off-topic.

@BerreYesilyurt
Copy link

I am using latest React Native Version 0.74.2. I have a mainactivity.kt inside android/app/src/main/java
how to fix this issue please help anyone

Have you tried adding this to your MainActivity.kt file?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(null)
}

When I tried this my app started crashing normally in addition to the background. Also i got a new error. I added the error below

WhatsApp Image 2024-09-04 at 09 13 12
WhatsApp Image 2024-09-04 at 09 13 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Missing repro This issue need minimum repro scenario Platform: Android This issue is specific to Android
Projects
None yet
Development

No branches or pull requests