Created
September 14, 2018 14:31
-
-
Save ValeryPonomarenko/14bab71e19a2e4698f05c630906e5dfd to your computer and use it in GitHub Desktop.
Lifecycle aware dagger components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class FragmentLifecycleHelper( | |
private val componentsStore: ComponentsStore | |
) : FragmentManager.FragmentLifecycleCallbacks() { | |
private var isInSaveState = false | |
override fun onFragmentStarted(fm: FragmentManager, f: Fragment) { | |
super.onFragmentStarted(fm, f) | |
isInSaveState = false | |
} | |
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) { | |
super.onFragmentResumed(fm, f) | |
isInSaveState = false | |
} | |
override fun onFragmentSaveInstanceState(fm: FragmentManager, f: Fragment, outState: Bundle) { | |
super.onFragmentSaveInstanceState(fm, f, outState) | |
isInSaveState = true | |
} | |
override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) { | |
super.onFragmentDestroyed(fm, f) | |
if (f !is IHasComponent) return | |
if (f.requireActivity().isFinishing) { | |
componentsStore.remove(f.javaClass.toString()) | |
return | |
} | |
if (isInSaveState) { | |
isInSaveState = false | |
return | |
} | |
var anyParentIsRemoving = false | |
var parent = f.parentFragment | |
while (!anyParentIsRemoving && parent != null) { | |
anyParentIsRemoving = parent.isRemoving | |
parent = parent.parentFragment | |
} | |
if (f.isRemoving || anyParentIsRemoving) { | |
componentsStore.remove(f.getComponentKey()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment