Last active
April 12, 2019 16:08
-
-
Save zawadz88/06a8b7d55b27ad7e563606f0cdddb0b7 to your computer and use it in GitHub Desktop.
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
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