简体   繁体   English

Dagger2在视图模型中注入导航器

[英]Dagger2 Inject a navigator in a viewmodel

I am using Android's ViewModel lib and I want to inject a Navigator in a ViewModel which is scoped inside App, but my navigator is dependent of activity. 我正在使用Android的ViewModel lib,我想在一个ViewModel中注入一个Navigator,它位于App的范围内,但我的导航器依赖于活动。 I don't know how to inject it. 我不知道如何注射它。 I'll copy and paste some files here but my project can be found here and the files I mention is under 'app/src/main/java/app/vehiclemonitor/' + 'app/' or 'viewmodel' or 'features/home' 我会复制并粘贴一些文件,但我的项目可以在这里找到,我提到的文件在'app / src / main / java / app / vehiclemonitor /'+'app /'或'viewmodel'或'features /家'

VmAppComponent.java VmAppComponent.java

@Singleton
@Component(modules = {VMAppModule.class, VMApiServiceModule.class, SchedulerModule.class, ViewModelModule.class})
public interface VMAppComponent {

    void inject(VMApp app);

    HomeActivityComponent injectHomeActivity(BaseActivityModule module);

    AddEditVehicleActivityComponent injectAddEditVehicleActivity(BaseActivityModule module);
}

ViewModelModule.class ViewModelModule.class

@Module
public abstract class ViewModelModule {

    @Binds
    @IntoMap
    @ViewModelKey(HomeViewModel.class)
    abstract ViewModel bindHomeViewModel(HomeViewModel homeViewModel);

    @Binds
    abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);

}


public class HomeViewModel extends ViewModel {

    @NonNull    
    private HomeNavigator navigationProvider;

    @NonNull
    private BaseSchedulerProvider schedulerProvider;

    @Inject
    public HomeViewModel(@NonNull final BaseSchedulerProvider schedulerProvider) {
        this.schedulerProvider = schedulerProvider;
    }

    // @Inject
    // public HomeViewModel(@NonNull HomeNavigator navigationProvider, @NonNull BaseSchedulerProvider schedulerProvider) {
    //  this.navigationProvider = navigationProvider;
    //  this.schedulerProvider = schedulerProvider;
    // }

    void handleAddButtonClick() {
        navigationProvider.addNewVehicle();
    }

    public void setNavigationProvider(final HomeNavigator navigationProvider) {
        this.navigationProvider = navigationProvider;
    }
}

First of all, this is a good question, a lot of developers had/have the same question and currently there isn't an official solution to this. 首先,这是一个很好的问题,很多开发人员都有同样的问题,目前还没有正式的解决方案。

The only sure thing is: 唯一可靠的是:

A ViewModel should not have references to Activities or Views in general. ViewModel通常不应引用活动或视图。

What you should do is reading all this thread in GitHub https://github.com/googlesamples/android-architecture-components/issues/63 , a lot of solutions have been posted and probably there is at least one good for you. 你应该做的是在GitHub中阅读所有这个帖子https://github.com/googlesamples/android-architecture-components/issues/63 ,已经发布了很多解决方案,并且可能至少有一个对你好。

About the link you posted on googlesamples and blueprint , their Navigator is not good as you can think. 关于你在googlesamples和蓝图上发布的链接,他们的Navigator并不像你想象的那么好。 It's still an example app and probably they removed some complexity to make the sample easier to understand for a wider set of developers. 它仍然是一个示例应用程序,可能他们删除了一些复杂性,使样本更容易理解为更广泛的开发人员。

I think a better approach is the one provided in this sample repo which completely remove references to Activities. 我认为更好的方法是在此样本仓库中提供的方法,它完全删除对活动的引用。 (It's made in Kotlin, I don't know if you can emulate the same behaviours with Java but you could try at least). (它是在Kotlin中制作的,我不知道你是否可以用Java模仿相同的行为但你至少可以尝试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM