Skip to content

Instantly share code, notes, and snippets.

@Pluu
Last active March 16, 2020 16:11
Show Gist options
  • Save Pluu/0ebfba44faec193b88fcb68e6e7ce25f to your computer and use it in GitHub Desktop.
Save Pluu/0ebfba44faec193b88fcb68e6e7ce25f to your computer and use it in GitHub Desktop.
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull String key, @NonNull Class<T> modelClass) {
boolean isAndroidViewModel = AndroidViewModel.class.isAssignableFrom(modelClass);
...
Constructor<T> constructor;
constructor = ... // Define ViewModel Constructor
// Case 1, SavedStateHandleが不要の場合
if (constructor == null) {
return mFactory.create(modelClass);
}
// Case 2, SavedStateHandleが必要な場合
SavedStateHandleController controller = SavedStateHandleController.create(
mSavedStateRegistry, mLifecycle, key, mDefaultArgs);
try {
T viewmodel;
// SavedStateHandleControllerを介してSavedStateHandleを注入
if (isAndroidViewModel) {
viewmodel = constructor.newInstance(mApplication, controller.getHandle());
} else {
viewmodel = constructor.newInstance(controller.getHandle());
}
...
return viewmodel;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment