MarioZ
MarioZ

Reputation: 193

React Native app crash when returning from camera on Android

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?

Upvotes: 12

Views: 2762

Answers (1)

Rodrigo Fernandes
Rodrigo Fernandes

Reputation: 1

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}},
    ],
  }),
);

Upvotes: 0

Related Questions