|
| 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 | +} |
0 commit comments