【发布时间】:2018-07-12 08:06:34
【问题描述】:
在努力遵循来自the authors themselves 的关于注入和避免繁琐代码(我有)的良好和官方建议时,我在尝试使用支持库时遇到了障碍。
根据文章:
AppCompat 用户应继续实施
AndroidInjector.Factory<? extends Activity> and not <? extends AppCompatActivity>(或FragmentActivity)。
我坚持使用视图始终为 Fragments 的 MVP 架构,我不想让我的 Activity 参与任何 DI 业务,但我想知道这是否有必要工作,但到目前为止我没能。如果我跳过整个支持,应用程序会在运行时崩溃,因为片段的实例是支持的(以防它不明显)。然后我开始尝试实现HasSupportFragmentInjector 而不是HasFragmentInjector 的任务,由于编译错误,为了我的心理健康,我的大脑已经忘记了这一点。过了一会儿,我开始思考非支持活动如何托管支持片段。啊!那些棘手的通配符。但是无论我如何尝试遵循建议,我都无法想出没有EmptyModule 的方法,我还需要在 Activity 中进行设置,以便通过 dagger 及其 (真的,对我来说仍然是魔法)。为什么我没试过?我也可以,但我厌倦了无望的变化,此时我需要帮助。
AppModule.kt
@Singleton
@dagger.Module
class AppModule(val application: Application) {
@Provides @Singleton fun application(): Application = application
...
}
AppComponent.java
@ApplicationScope
@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
...
FooFragmentModule.class,
})
public interface AppComponent {
Application app();
...
void inject(MyApp app);
}
MyApp.java
public class MyApp extends Application implements HasActivityInjector {
private AppComponent component;
public AppComponent someWayToReturnAppComponent() {
...
}
@Inject DispatchingAndroidInjector<Activity> dispatchingActivityInjector;
@Override
public void onCreate() {
component = DaggerAppComponent.builder()
.appModule(new AppModule(this))
// more app-scoped modules
.build();
component.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingActivityInjector;
}
}
MainActivity.java
public abstract class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout()); // inflate the fragment via XML here
}
@Inject DispatchingAndroidInjector<Fragment> dispatchingFragmentInjector;
@Override
public AndroidInjector<Fragment> fragmentInjector() {
return dispatchingFragmentInjector;
}
}
FooFragmentComponent.java
@Subcomponent
public interface FooFragmentComponent extends AndroidInjector<FooFragment> {
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<FooFragment> {}
}
FooFragmentModule.kt
@dagger.Module(subcomponents = {FooFragmentComponent.class})
public abstract class FooFragmentModule {
@Binds
@IntoMap
@FragmentKey(FooFragment.class)
abstract AndroidInjector.Factory<? extends Fragment> bindFragmentInjectorFactory(FooFragmentComponent.Builder builder);
@ActivityScope
abstract FooFragment contributeFooFragmentInjector();
@Provides
static FooPresenter presenter() {
return new FooPresenter();
}
}
FooFragment
public class FooFragment extends Fragment implements SomeView {
@Inject FooPresenter presenter;
}
好的。至此,再回到
AppCompat 用户应继续实施
AndroidInjector.Factory<? extends Activity>
我没有必要(并且愿意反对)使用它,仅用于片段。我真的需要为它设置一个模块和组件还是我遗漏了什么?
编辑
在遵循 EpicPandaForce 的使用 AndroidSupportInjectionModule 的建议后,Dagger 现在抱怨说
FragmentKey 方法应该绑定
dagger.android.AndroidInjector.Factory<? extends android.app.Fragment>,而不是dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>。
【问题讨论】:
-
使用
AndroidSupportInjectionModule -
@EpicPandaForce 我做了,但仍然遇到问题。感谢您提供进一步的反馈。
标签: android dependency-injection android-support-library dagger