• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    koin for android (arc viewmodel)
    https://beta.insert-koin.io/docs/2.0/quick-references/koin-for-android/
    
    Android Architecture ViewModel (koin-android-viewmodel or koin-androidx-viewmodel projects)
    Koin brings special features to manage ViewModel:
    
    viewModel special DSL keyword to declare a ViewModel
    by viewModel() & getViewModel() to inject ViewModel instance (from Activity & Fragment)
    by sharedViewModel() & getSharedViewModel() to reuse ViewModel instance from hosting Activity (from Fragment)
    Let’s declare a ViewModel in a module:
    
    val myModule : Module = applicationContext {
        
        // ViewModel instance of MyViewModel
        // get() will resolve Repository instance
        viewModel { MyViewModel(get()) }
    
        // Single instance of Repository
        single<Repository> { MyRepository() }
    }
    
    Inject it in an Activity:
    
    
    class MyActivity : AppCompatActivity(){
    
        // Lazy inject MyViewModel
        val model : MyViewModel by viewModel()
    
        override fun onCreate() {
            super.onCreate()
    
            // or also direct retrieve instance
            val model : MyViewModel = getViewModel()
        }
    }

    Comment


    입력하3 1560962871



    Locations of visitors to this page