Last active
November 12, 2019 15:30
-
-
Save kirich1409/6d129d9c12143981e84b5cf54d0dd8e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class SetFragmentFactoryActivityCallback( | |
private val newFragmentFactory: FragmentFactory | |
) : EmptyActivityLifecycleCallbacks { | |
private val fragmentLifecycleCallbacks = | |
SetFragmentFactoryFragmentCallback(newFragmentFactory) | |
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | |
if (activity is FragmentActivity) { | |
val fragmentManager = activity.supportFragmentManager | |
fragmentManager.fragmentFactory = newFragmentFactory | |
fragmentManager.registerFragmentLifecycleCallbacks( | |
fragmentLifecycleCallbacks, | |
true // recursevly register the callbacks for children fragments | |
) | |
} | |
} | |
} | |
private class SetFragmentFactoryFragmentCallback( | |
private val newFragmentFactory: FragmentFactory | |
) : FragmentManager.FragmentLifecycleCallbacks() { | |
override fun onFragmentPreAttached( | |
fm: FragmentManager, f: Fragment, context: Context | |
) { | |
f.childFragmentManager.fragmentFactory = fragmentFactory | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment