How to use dependency injection for injecting constructor in a ViewModel

后端 未结 2 1050
灰色年华
灰色年华 2020-12-10 20:31

I am trying to implement the example on https://developer.android.com/jetpack/docs/guide. This explains how tan android app should be structured.

When I use the same

2条回答
  •  -上瘾入骨i
    2020-12-10 20:58

    You can use Koin as an alternative for Dagger. Koin have full support of Android Architecture ViewModel. It's extremely easy to use. I prefer it for small and mid-sized project.

    // declared ViewModel using the viewModel keyword
    val myModule : Module = module {
        viewModel { MyViewModel(get()) } 
        ...
    }
    
    // Just get it
    class MyActivity() : AppCompatActivity() {
    
        // lazy inject MyViewModel
        val myViewModel : MyViewModel by viewModel()
    }
    

    insert-koin.io

提交回复
热议问题