daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // used classes
  2. class MyRepository()
  3. class MyViewModel(val repository : MyRepository) : ViewModel()
  4.  
  5. // module
  6. val myModule : Module = applicationContext {
  7.    
  8.     // ViewModel instance of MyViewModel
  9.     // get() will resolve Repository instance
  10.     viewModel { MyViewModel(get()) }
  11.     // or even (with builder API)
  12.     viewModel<MyViewModel>()
  13.  
  14.     // Single instance of Repository
  15.     single<Repository> { MyRepository() }
  16. }
  17.  
  18. // Use directly your ViewModel  
  19. class MyActivity : AppCompatActivity(){
  20.  
  21.     // Lazy inject MyViewModel
  22.     val model : MyViewModel by viewModel()
  23.  
  24.     override fun onCreate() {
  25.         super.onCreate()
  26.  
  27.         // or direct retrieve instance
  28.         val model : MyViewModel = getViewModel()
  29.     }
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top