Skip to content

Instantly share code, notes, and snippets.

@zawadz88
Last active April 12, 2019 16:08
Show Gist options
  • Save zawadz88/06a8b7d55b27ad7e563606f0cdddb0b7 to your computer and use it in GitHub Desktop.
Save zawadz88/06a8b7d55b27ad7e563606f0cdddb0b7 to your computer and use it in GitHub Desktop.
class InjectingFragmentFactory
@Inject constructor(
private val creators: Map<Class<out Fragment>, @JvmSuppressWildcards Provider<Fragment>>
) : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
val fragmentClass = loadFragmentClass(classLoader, className)
val creator = creators[fragmentClass]
?: return createFragmentAsFallback(classLoader, className)
try {
return creator.get()
} catch (e: Exception) {
throw RuntimeException(e)
}
}
private fun createFragmentAsFallback(classLoader: ClassLoader, className: String): Fragment {
Timber.w("No creator found for class: $className. Using default constructor")
return super.instantiate(classLoader, className)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment