Instantly launch ViewModels directly without using the ViewModelProviders.of method

I have a ViewModel named RecipesViewModel

. I used to create it like this:

RecipesViewModel viewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() {
            @Override
            public <T extends ViewModel> T create(Class<T> modelClass) {
                return (T) new RecipesViewModel(recipesRepository);
            }
        }).get(RecipesViewModel.class);

      

But now I am using dagger2 and so I put the annotation @Inject

in the constructor of this ViewModel so I can inject it directly into my fragment using the field injector.

My question is, am I missing something by starting the path to the viewmodel, not ViewModelProviders.of

? My ViewModel already has Scoped scope, so only one instance is created in the context.

Another option is to move only the factory instance to dagger2 module, but if no problem, I prefer the first aproach.

- EDIT -

Reading the documentation android.arch.lifecycle.ViewModel

, I am a little scared. Use ViewModelProviders to provide a scope (fragment or action). If I create an instance myself, what will the scope be?

ViewModel is a class that is responsible for preparing and managing data for an Activity or Fragment. It also handles the activity / fragment message with the rest of the application (such as calling the business logic classes).

The ViewModel is always created along with the scope (fragment or activity) and will persist as long as the scope is alive. For example. if this is an action until it is completed.

In other words, this means that the ViewModel will not be destroyed if its owner is destroyed to change the configuration (eg rotation). the new owner instance will simply re-attach to the existing ViewModel.

- / EDIT -

Below is the code for the RecipesViewModel:

@PerActivity
public class RecipesViewModel extends ViewModel {
    private static final String TAG = "RecipesViewModel";
    private final RecipesRepository recipesRepository;

    private LiveData<List<Recipe>> recipes = null;

    @Inject
    public RecipesViewModel(RecipesRepository recipesRepository) {
        this.recipesRepository = recipesRepository;
    }

    public final void loadAll() {
        recipes = recipesRepository.getRecipes();
    }

    public LiveData<List<Recipe>> getRecipes() {
        return recipes;
    }
}

      

+3


source to share


1 answer


For me right now (and I need to research this), but introducing a ViewModel instead of using the ViewModelProviders function means you lose the easy link to Action Fragments.



For example, from the docs, they provide an example of an activity containing 2 fragments. If one chunk needed to talk to another, the previous method was to keep the interface alive through the activity, which also had to keep track of the lifecycle of that interface. Instead, you can now simply pull it out of the ViewModelProviders repository whenever you need.

+1


source







All Articles