Created
May 25, 2018 17:58
-
-
Save joseprl89/1418921b2409e436a80f9f2f7e87d4c2 to your computer and use it in GitHub Desktop.
How ViewModelProvider works for https://medium.com/@josep.rodriguez/internals-of-android-architecture-components-part-i-the-viewmodel-d893e362a0d9
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
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) { | |
String canonicalName = modelClass.getCanonicalName(); | |
if (canonicalName == null) { | |
throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels"); | |
} | |
return get(DEFAULT_KEY + ":" + canonicalName, modelClass); | |
} | |
public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) { | |
ViewModel viewModel = mViewModelStore.get(key); | |
if (modelClass.isInstance(viewModel)) { | |
return (T) viewModel; | |
} else { | |
... | |
} | |
viewModel = mFactory.create(modelClass); | |
mViewModelStore.put(key, viewModel); | |
return (T) viewModel; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment