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

Latest commit

 

History

History
37 lines (25 loc) · 993 Bytes

README.md

File metadata and controls

37 lines (25 loc) · 993 Bytes

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)
    
        // ...
    }
}