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.

How to inject viewModel in BaseFragment in Koin

See original GitHub issue

I have created an abstract BaseFragment class which will be extended by other concrete Fragment classes. I want to inject ViewModel in my BaseFragment using Koin. Here is my BaseFragment:

abstract class BaseFragment<out VM : BaseViewModel, DB : ViewDataBinding>(private val mViewModelClass: Class<VM>) : Fragment() {

    val viewModel: VM by viewModel()

    open lateinit var binding: DB

    fun init(inflater: LayoutInflater, container: ViewGroup) {
        binding = DataBindingUtil.inflate(inflater, getLayoutRes(), container, false)
    }

    open fun init() {}
    @LayoutRes
    abstract fun getLayoutRes(): Int

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        init(inflater, container!!)
        init()
        super.onCreateView(inflater, container, savedInstanceState)
        return binding.root
    }

    open fun refresh() {}
} 

But I am not able to do so. I am using 2.0.1 version of Koin

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
arcaocommented, Jun 29, 2019

Something like this?

abstract class BaseFragment<VM : BaseViewModel, DB : ViewDataBinding>() : Fragment() {
    protected abstract val viewModel: VM
    @LayoutRes
    protected abstract val layoutRes: Int
    
    protected lateinit var binding: DB

    fun init(inflater: LayoutInflater, container: ViewGroup) {
        binding = DataBindingUtil.inflate(inflater, layoutRes, container, false)
    }

    open fun init() {}

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        init(inflater, container!!)
        init()
        return binding.root
    }

    open fun refresh() {}
} 

class SampleFragment : BaseFragment<SampleVM, FragmentSampleBinding>() {
    override val viewModel by inject<SampleVM>()
    override val layouRes = R.layout.fragment_sample
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to inject viewModel in BaseFragment using Koin
I want to inject ViewModel in my BaseFragment using Koin . Here is my BaseFragment: abstract class BaseFragment<out VM : BaseViewModel, ...
Read more >
Android ViewModel & Navigation | Koin
ViewModel class. You can specify how you inject the constructor of the class and use the get() function to inject dependencies.
Read more >
How to inject viewModel in BaseFragment using Koin-kotlin
Add your ViewModel as an abstract and set value when you extend your BaseFragment. My BaseFragment have: abstract class BaseFragment<Binding ...
Read more >
Dependency Injection using Koin - Medium
Koin is the dependency injection framework purely built in Kotlin. ... class LandingFragment : BaseFragment() { //View model injection using ...
Read more >
Dagger2 injection doesn't happen when the ... - Reddit
Hi there, My app has a BaseFragment where I intend to keep all ... the ViewModel to the NavBackStackEntry of that nested navigation...
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