Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Latest commit

 

History

History

androidx-fragment

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Fragment constructor injection with FragmentFactory

This artifact provides additional support for androidx.fragment enabling constructor injection in Fragments with KatanaFragmentFactory.

// In a module: 

val module = Module {

    singleton {
        KatanaFragmentFactory(component)
            .handlesFragment<FirstFragment>()
            .handlesFragment<SecondFragment>(name = "SecondFragment")
    }

    factory { FirstFragment(get()) }

    factory(name = "SecondFragment") { SecondFragment(get(), get()) }
}

// ... then in an Activity:

class MyActivity : AppCompatActivity() {

    private val fragmentFactory by applicationComponent.inject<KatanaFragmentFactory>()

    override fun onCreate(savedInstanceState: Bundle?) {
        // Must be set **before** super call for Fragment instantiation after orientation change
        supportFragmentManager.fragmentFactory = fragmentFactory

        super.onCreate(savedInstanceState)
    
        // ...
    }
}