List of questions
Related questions
Question 78 - Associate Android Developer discussion
An example. In our ViewModelFactory (that implements ViewModelProvider.Factory) we have an instance of our Repository, named mRepository. Our ViewModel has such constructor:
public MyViewModel(MyRepository myRepository)...
Next, in our ViewModelFactory create ViewModel method (overriden) looks like this: @NonNull @Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { try {
//MISSED RETURN VALUE HERE
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
}}
What should we write instead of "//MISSED RETURN VALUE HERE"?
A.
return modelClass.getConstructor().newInstance(mRepository);
B.
return modelClass.getConstructor(MyRepository.class).newInstance();
C.
return modelClass.getConstructor(MyRepository.class).newInstance(mRepository);
Your answer:
0 comments
Sorted by
Leave a comment first