SHARE
TWEET

Untitled

a guest Apr 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class InjectingFragmentFactory
  2. @Inject constructor(
  3.     private val creators: Map<Class<out Fragment>, @JvmSuppressWildcards Provider<Fragment>>
  4. ) : FragmentFactory() {
  5.  
  6.     override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
  7.         val fragmentClass = loadFragmentClass(classLoader, className)
  8.         val creator = creators[fragmentClass]
  9.             ?: return createFragmentAsFallback(classLoader, className)
  10.  
  11.         try {
  12.             return creator.get()
  13.         } catch (e: Exception) {
  14.             throw RuntimeException(e)
  15.         }
  16.     }
  17.  
  18.     private fun createFragmentAsFallback(classLoader: ClassLoader, className: String): Fragment {
  19.         Timber.w("No creator found for class: $className. Using default constructor")
  20.         return super.instantiate(classLoader, className)
  21.     }
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top