Fragment Factory
Koin integrates with AndroidX FragmentFactory to enable constructor injection in Fragments.
info
Fragment Factory uses DSL only. Annotation and Compiler Plugin DSL support is not yet available.
Setup
Add Dependency
implementation "io.insert-koin:koin-android:$koin_version"
Configure Fragment Factory
In your Koin configuration, enable the fragment factory:
startKoin {
androidContext(this@MainApplication)
fragmentFactory()
modules(appModule)
}
Declaring Fragments
Use the fragment DSL keyword with constructor injection:
class MyFragment(
private val myService: MyService
) : Fragment()
val appModule = module {
single { MyService() }
fragment { MyFragment(get()) }
}
Using Fragments
Setup in Activity
Call setupKoinFragmentFactory() before super.onCreate():
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Must be called BEFORE super.onCreate()
setupKoinFragmentFactory()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Add Fragment
Use the reified extension function:
supportFragmentManager.beginTransaction()
.replace<MyFragment>(R.id.container)
.commit()
With arguments and tag:
supportFragmentManager.beginTransaction()
.replace<MyFragment>(
containerViewId = R.id.container,
args = bundleOf("key" to "value"),
tag = "my_fragment"
)
.commit()
Fragment Factory with Scopes
To use Activity-scoped dependencies in your Fragment:
val appModule = module {
scope<MyActivity> {
scoped { ActivityService() }
fragment { MyFragment(get()) }
}
}
Pass the scope to setupKoinFragmentFactory():
class MyActivity : AppCompatActivity(), AndroidScopeComponent {
override val scope: Scope by activityScope()
override fun onCreate(savedInstanceState: Bundle?) {
// Pass scope to fragment factory
setupKoinFragmentFactory(scope)
super.onCreate(savedInstanceState)
}
}
Quick Reference
| Action | Code |
|---|---|
| Declare fragment | fragment { MyFragment(get()) } |
| Setup global factory | setupKoinFragmentFactory() |
| Setup with scope | setupKoinFragmentFactory(scope) |
| Add fragment | .replace<MyFragment>(R.id.container) |
Next Steps
- AndroidX Fragment - Official Fragment documentation
- Scopes - Android scopes
- ViewModel - ViewModel injection