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: Fix fragment resume crashes #1117

Merged

Conversation

djkazic
Copy link
Collaborator

@djkazic djkazic commented Jan 17, 2023

On Android the View state is not persisted consistently across Activity restarts, which can lead to crashes in those cases. It is recommended to override the native Android method called on Activity restarts in your main Activity, to avoid these crashes.

For most people using an app built from the react-native template, that means editing MainActivity.java, likely located in android/app/src/main/java//MainActivity.java

You should add this code, which specifically discards any Activity state persisted during the Activity restart process, to avoid inconsistencies that lead to crashes. Please note that the override code should not be placed inside MainActivityDelegate, but rather directly in MainActivity.


public class MainActivity extends ReactActivity {

    //...code

    //react-native-screens override
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(null);
    }

    public static class MainActivityDelegate extends ReactActivityDelegate {
        //...code
    }
}```

@vercel
Copy link

vercel bot commented Jan 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
blixt-wallet ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Jan 17, 2023 at 11:22PM (UTC)

@djkazic
Copy link
Collaborator Author

djkazic commented Jan 17, 2023

More info here: https://github.com/software-mansion/react-native-screens#android
software-mansion/react-native-screens#17

Re: why this is nulled out instead of used:
https://twitter.com/ferrannp/status/1126056440924389376
Android fragments have built-in view hierarchy persistence by default. It is turned off in screens however. I believe this is outside of the scope of this project. The problem here is that there is no value in having your view hierarchy restored if the underlying react app does not restore to the same state. This way you'd have a UI out of sync with the application state. The way it should work is that app state persistence should be handled on the react side, such that when the app gets restored react app first recreates the state and only after that native view hierarchy should be recreated to match that state (this may not always be possible and in fact this is the same for native apps, I had many issues with that automatic mechanism in the past when working on Android native apps because normally only a fraction of the app state is kept in the view hierarchy). React works terribly with rehydration even in React DOM. Highly doubt it's possible to make it work reliably here.
software-mansion/react-native-screens#96 (comment)
software-mansion/react-native-screens#96 (comment)

@hsjoberg
Copy link
Owner

LGTM! We probably shouldn't use the savedInstanceState arg.

@hsjoberg hsjoberg changed the title Fix fragment resume crashes android: Fix fragment resume crashes Jan 20, 2023
@hsjoberg hsjoberg merged commit 6eb2978 into hsjoberg:master Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants