ExamGecko
Question list
Search
Search

List of questions

Search

Related questions











Question 78 - Associate Android Developer discussion

Report
Export

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);
Answers
A.
return modelClass.getConstructor().newInstance(mRepository);
B.
return modelClass.getConstructor(MyRepository.class).newInstance();
Answers
B.
return modelClass.getConstructor(MyRepository.class).newInstance();
C.
return modelClass.getConstructor(MyRepository.class).newInstance(mRepository);
Answers
C.
return modelClass.getConstructor(MyRepository.class).newInstance(mRepository);
Suggested answer: C
asked 18/09/2024
André Batista
39 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first