Skip to content

Instantly share code, notes, and snippets.

@dshen6
Created March 12, 2018 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshen6/6e0dc723678d8bbb2e40ea3b3378a16b to your computer and use it in GitHub Desktop.
Save dshen6/6e0dc723678d8bbb2e40ea3b3378a16b to your computer and use it in GitHub Desktop.
Android Arch-ViewModelProviderHelper
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelStores
import android.support.v4.app.Fragment
/**
* Created by Shen on 2/4/2018.
*/
object ViewModelProviderHelper {
fun forFragmentInActivity(fragment: Fragment): ViewModelProvider {
val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(fragment.activity.application)
val store = ViewModelStores.of(fragment.activity)
return ViewModelProvider(store, factory)
}
fun forFragment(fragment: Fragment): ViewModelProvider {
val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(fragment.activity.application)
val store = ViewModelStores.of(fragment)
return ViewModelProvider(store, factory)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment