Inject a ViewModel in a BottomSheetDialogFragment JAVA

546 Views Asked by At

I'm using dagger2 in my application and I'm trying to inject a ViewModel into BottomSheetDialogFragment but I don't know how.

I have the BaseApplication class like this:

public class BaseApplication extends DaggerApplication {

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerAppComponent
            .builder()
            .application(this)
            .build();
    }
}

And the ViewModelFactory:

@Module
public abstract class ViewModelFactoryModule {
    @Binds
    public abstract ViewModelProvider.Factory mBindViewModelFactory(ViewModelProviderFactory mModelProviderFactory);
}

When I try to inject the viewmodel inside the BottomSheetDialogFragment it shows null exception

public class BottomSheetMoreOptions extends BottomSheetDialogFragment {
     @Inject
     FeedViewModel ViewModel;
} 

The ViewModel constructor is:

@Inject
public FeedViewModel(@Named("notificationsRef") DatabaseReference mRef) {
    Log.d(TAG, "HomeViewModel: is ready...");
    this.mRef = mRef;
}

Actually to load the bottom sheet I'm passing the view model in its constructor:

BottomSheetMoreOptions bottomSheetMoreOptions = new BottomSheetMoreOptions(model.getFeed().getId(), viewModel);
            bottomSheetMoreOptions.show(requireActivity().getSupportFragmentManager(),
                    "ModalBottomSheet");

Any help please in Java?

0

There are 0 best solutions below