Skip to content

Instantly share code, notes, and snippets.

@joseprl89
Created May 25, 2018 17:58
Show Gist options
  • Save joseprl89/1418921b2409e436a80f9f2f7e87d4c2 to your computer and use it in GitHub Desktop.
Save joseprl89/1418921b2409e436a80f9f2f7e87d4c2 to your computer and use it in GitHub Desktop.
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