React Native app crash when returning from camera on Android
Asked Answered
P

1

12

I have a problem that seems to be pretty common, but I haven't found a solution yet. I'm using react-navigation, react-native-screens and react-native-image-picker. That last plugin allows the app to take photos and load photos from gallery. It works most of the time, but sometimes it crashes after taking a photo and returning to the app. I searched for a solution on their GitHub, and there are many reports of this crash, but not a single fix. This crash was also reported in another library, react-native-image-crop-picker, so it doesn't seem to be a problem with the plugin, but with Android.

This is what I got from Firebase crash logs:

  • Unable to start activity ComponentInfo{com.myApp/com.myApp.MainActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.swmansion.rnscreens.ScreenStackFragment
  • Unable to instantiate fragment com.swmansion.rnscreens.ScreenStackFragment
  • 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.

This seems to be related to how Android kills background apps when it needs more memory. That last error has a link to a Github issue. They recommend modifying MainActivity.java -> onCreate(), and change super.onCreate(savedInstanceState); to super.onCreate(null);. However, this doesn't fix anything, it just makes the app restart instead of crashing, but I still lose the entire navigation stack, and the photo I just took. Is there any proper fix for this problem?

Plumate answered 20/1, 2021 at 17:47 Comment(2)
Hi. Did you ever find a solution to this? I have been struggling with this for months...Complected
@KevinGardenhire No, sorry. Every library that puts your app in background seem to have this problem. I ended up using a different library to implement my own camera inside the app. Can't remember which one I used for that specific project, but I recently used expo-camera and it worked fine (I had to convert my project into bare expo)Plumate
L
0

In any application button that leads to another page, instead of using navigate, use dispatch.reset, it is a problem related to the state of react navigation.

this.props.navigation.dispatch(
  CommonActions.reset({
    index: 1,
    routes: [
      {name: 'AtualScreen'},
      {name: 'NextScreen', params: {data: data}},
    ],
  }),
);
Lucaslucca answered 23/12, 2022 at 21:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.