Last active
March 16, 2020 16:11
-
-
Save Pluu/0ebfba44faec193b88fcb68e6e7ce25f to your computer and use it in GitHub Desktop.
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
@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