Skip to content

Instantly share code, notes, and snippets.

@Pluu
Created March 16, 2020 15:56
Show Gist options
  • Save Pluu/c1de6e10a1d768f5b960353018402a8a to your computer and use it in GitHub Desktop.
Save Pluu/c1de6e10a1d768f5b960353018402a8a to your computer and use it in GitHub Desktop.
public class ViewModelProvider {
...
private final Factory mFactory;
private final ViewModelStore mViewModelStore;
// ① Use ViewModelStoreOwner
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory
? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()
: NewInstanceFactory.getInstance());
}
// ② Use ViewModelStoreOwner, Factory
public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {
this(owner.getViewModelStore(), factory);
}
// ③ Use ViewModelStore, Factory
public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
mFactory = factory;
mViewModelStore = store;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment