Last active
February 21, 2020 09:52
Пример реализации FragmentFactory из androidx.fragment 1.1.0, позовляющих доставлять зависимости в конструктор.
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
// Описываем зависимости в констркуторе, они сразу final и private | |
class MainFragment(private val dependency: SampleDependency) : Fragment() |
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
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) | |
@Retention(AnnotationRetention.SOURCE) | |
@MapKey | |
annotation class FragmentKey(val clazz: KClass<out Fragment>) |
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
// Описываем Dagger 2 модуль с описанием зависимости для Multibinding | |
// https://google.github.io/dagger/multibindings | |
@Module | |
interface FragmentModule { | |
@Binds @IntoMap @FragmentKey(MainFragment::class) | |
fun bindListViewModel(fragment: MainFragment): Fragment | |
} |
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 DaggerFragmentFactory @Inject constructor( | |
private val providers: Map<Class<out Fragment>, @JvmSuppressWildcards Provider<Fragment>> | |
) : FragmentFactory() { | |
override fun instantiate(classLoader: ClassLoader, className: String, args: Bundle?): Fragment { | |
val fragmentProvider = providers[loadFragmentClass(classLoader, className)] | |
?: return super.instantiate(classLoader, className, args) | |
val fragment: Fragment = fragmentProvider.get() | |
fragment.arguments = args | |
return fragment | |
} | |
} |
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 MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// Выполняем inject зависимостей в MainActivity | |
setContentView(R.layout.activity_main) | |
} | |
// Вместо inject в поле используем inject в метод, что подходит для случаев, | |
// когда зависимость нужна только для инициализации | |
@Inject | |
fun initFragmentFactory(fragmentFactory: DaggerFragmentFactory) { | |
supportFragmentManager.fragmentFactory = fragmentFactory | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment