question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[AndroidX] Introducing koin-androidx-fragment with Koin fragmentFactory

See original GitHub issue

Hi All,

the 2.1.0-alpha-3 contains a new package: koin-androidx-fragment

This will allow to inject your Fragment with Koin 👍

How to use that?

  1. setup your KoinFragmentFactory at start with fragmentFactory():
 startKoin {
    androidLogger(Level.DEBUG)
    androidContext(this@MainApplication)

    // setup a KoinFragmentFactory instance
    fragmentFactory()

    modules(...)
}
  1. Use constructor for you Fragment class:
class MyFragment(val myService: MyService) : Fragment()
  1. Declare it in Koin (factory or scoped instance):
val appModule = module {
    
    factory { MyFragment(get()) }
}
  1. Setup the factory from your activity & just use it:

Setup the Koin factory from your activity:

class MyActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        // Koin Fragment Factory
        setupKoinFragmentFactory()

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

Get your Fragment:

supportFragmentManager.beginTransaction()
            .replace(R.id.mvvm_frame, MyFragment::class.java, null, null)
            .commit()

For more details (like using it with scope): https://doc.insert-koin.io/#/koin-android/fragment-factory

The package should be available soon 🎉

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
arnaudgiulianicommented, Nov 25, 2019

nothing, manifest is buggy in alpha-3. Check alpha-4 soon this week for a fix 👍

1reaction
mqueiroz1995commented, Nov 23, 2019

Amazing work!

For some reason, the package contains org.koin.androidx.viewmodel.BuildConfig.

This results in the following error when adding both koin-androidx-fragment and koin-androidx-viewmodel as dependencies:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class org.koin.androidx.viewmodel.BuildConfig found in modules classes.jar (org.koin:koin-androidx-fragment:2.1.0-alpha-3) and classes.jar (org.koin:koin-androidx-viewmodel:2.1.0-alpha-3)

Am I doing anything wrong?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Benefit from AndroidX FragmentFactory with Koin
Implementing custom FragmentFactory powered by Koin. Brief look on a new Android API FragmentFactory and look how we can use it and achieve ......
Read more >
Fragment Factory - Koin
Since AndroidX has released androidx.fragment packages family to extend features ... Since 2.1.0-alpha-3 version, has been introduce the FragmentFactory ...
Read more >
Koin Fragmentfactory Fails To Handle Activity Recreation
To declare a Fragment instance just declare it as a fragment in your Koin module and use constructor injection.Given a Fragment class: Copy.class...
Read more >
AndroidX FragmentFactory and Android Fragments - Medium
Fragments provide core Android app functionality. With the introduction of AndroidX in Android Jetpack, fragments got an overhaul and under the covers, ...
Read more >
CHANGELOG.md · Gitee 极速下载/koin - Gitee.com
koin-androidx-fragment. [FIXED] Add fallback for instantiate function in FragmentFactory - https://github.com/InsertKoinIO/koin/pull/742. koin-test.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found