Skip to content

Commit ad55264

Browse files
committed
fix: add review changes
1 parent 3a00e58 commit ad55264

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.swmansion.rnscreens
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.fragment.app.Fragment
8+
9+
/**
10+
* This class serves as a workaround to https://github.com/software-mansion/react-native-screens/issues/17.
11+
*
12+
* This fragment, when attached to the fragment manager & its state is progressed
13+
* to `ON_CREATED`, attempts to detach itself from the parent fragment manager
14+
* as soon as possible.
15+
*
16+
* Instances of this type should be created in place of regular screen fragments
17+
* when Android restores fragments after activity / application restart.
18+
* If done so, it's behaviour can prevent duplicated fragment instances,
19+
* as React will render new ones on activity restart.
20+
*/
21+
class AutoRemovingFragment: Fragment() {
22+
23+
override fun onCreate(savedInstanceState: Bundle?) {
24+
super.onCreate(savedInstanceState)
25+
26+
// This is the first moment where we have access to non-null parent fragment manager,
27+
// so that we can remove the fragment from the hierarchy.
28+
parentFragmentManager.beginTransaction()
29+
.remove(this)
30+
.commitAllowingStateLoss()
31+
}
32+
33+
override fun onCreateView(
34+
inflater: LayoutInflater,
35+
container: ViewGroup?,
36+
savedInstanceState: Bundle?
37+
): View? = null
38+
}

android/src/main/java/com/swmansion/rnscreens/NoOpFragment.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.

android/src/main/java/com/swmansion/rnscreens/RNScreensFragmentFactory.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package com.swmansion.rnscreens
22

33
import androidx.fragment.app.Fragment
44
import androidx.fragment.app.FragmentFactory
5-
import com.swmansion.rnscreens.gamma.helpers.NoOpFragment
65

76
class RNScreensFragmentFactory : FragmentFactory() {
87
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
98
return if (className.startsWith(BuildConfig.LIBRARY_PACKAGE_NAME)) {
10-
NoOpFragment();
9+
AutoRemovingFragment();
1110
} else {
1211
super.instantiate(classLoader, className)
1312
}
1413
}
15-
}
14+
}

0 commit comments

Comments
 (0)