Created
March 12, 2018 05:17
-
-
Save dshen6/6e0dc723678d8bbb2e40ea3b3378a16b to your computer and use it in GitHub Desktop.
Android Arch-ViewModelProviderHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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