Commit 93cee67
fix(Android): add custom fragment factory & modify library installation steps to prevent on-restoration crashes (#3089)
## Description
<!--
Description and motivation for this PR.
Include Fixes #<number> if this is fixing some issue.
Fixes # .
-->
This PR replaces the current workaround for crashes connected to the
configuration changes on Android. The previous fix required disabling
passing saved instance state in Activity onCreate - `onCreate(null)`.
Such a solution generates a problem with state persistence on the native
side of brownfield applications. Because of that, we decided to replace
it with a custom fragment factory that will limit the scope of the
solution and fix the problem directly only on the React Native side. All
fragments connected to the RN Screens package will be replaced with
self-destructing ones, which will prevent Android from recreating the
Fragment after the configuration change. We will leave this
responsibility for React Native. Thanks to that, there won't be any
conflict between the Android system and React Native, and we will
prevent the app from crashing or any other app appearance-related
issues.
Credits to @bojanlozanovski77 for bringing up the idea in
#2917
Fixes:
#17
## Changes
<!--
Please describe things you've changed here, make a **high level**
overview, if change is simple you can omit this section.
For example:
- Updated `about.md` docs
-->
- Created a custom fragment factory (RNScreensFragmentFactory.kt) that
won't instantiate RN Screens fragments
- Created self-destructing fragments (NoOpFragment.kt) that will be
instanciated by the RNScreensFragmentFactory, to prevent the conflict
that crashes the app
- Updated installation steps in library README
- Applied the fix in `MainActivity` files of `FabricExample` and
`Example` apps
<!--
## Screenshots / GIFs
Here you can add screenshots / GIFs documenting your change.
You can add before / after section if you're changing some behavior.
### Before
### After
-->
## Test code and steps to reproduce
<!--
Please include code that can be used to test this change and short
description how this example should work.
This snippet should be as minimal as possible and ready to be pasted
into editor (don't exclude exports or remove "not important" parts of
reproduction example)
-->
1. Remove ignoring config changes in `AndroidManifest.xml` file
```diff
<activity
android:name=".MainActivity"
android:label="@string/app_name"
- android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
android:launchMode="singleTask"
...
```
2. Launch the example app
3. Verify if the app isn't crashing when causing different types of
configuration changes on Android. For example:
- Orientation change on devices ≥600dp with portrait lock
- Opening/closing foldables or flip phones
- Split/Multi-Window mode
- Desktop windowing on tablets
- Launching external activities (e.g. system Photo Picker)
- Changing accessibility settings, permissions, system fonts,
wallpapers, or language
## Checklist
- [X] Included code example that can be used to test this change
- [X] Updated TS types
- [X] Updated documentation: <!-- For adding new props to native-stack
-->
- [X]
https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md
- [X]
https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md
- [X]
https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx
- [X]
https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx
- [X] Ensured that CI passes
---------
Co-authored-by: bojanlozanovski77 <[email protected]>
Co-authored-by: Kacper Kafara <[email protected]>1 parent ed11286 commit 93cee67
File tree
5 files changed
+66
-2
lines changed- Example/android/app/src/main/java/com/swmansion/rnscreens/example
- FabricExample/android/app/src/main/java/com/fabricexample
- android/src/main/java/com/swmansion/rnscreens/fragment/restoration
5 files changed
+66
-2
lines changedLines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | | - | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
22 | 29 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | | - | |
| 47 | + | |
| 48 | + | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
| 72 | + | |
69 | 73 | | |
70 | 74 | | |
71 | 75 | | |
| |||
Lines changed: 38 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
0 commit comments