如何使用dagger-android将片段与演示者相互链接?

时间:2018-03-31 20:48:23

标签: android android-fragments mvp dagger-2 dagger

我想要一个片段模块,它提供一个视图(片段)和演示者。视图取决于演示者,演示者取决于视图,而匕首负责这些依赖性。

我已经看到这种方法适用于活动和演示者,但是当无法将此模式应用于片段时。我想知道这是由于我的代码中的错误,还是匕首的限制。

以下是我用于活动的基线代码:

主要活动合同

public interface MainActivityContract {
    interface View {
        void setText(String text);
    }

    interface Presenter {
        void attach();
    }
}

主要活动模块

@Module
public abstract class MainActivityModule {

    @Binds
    abstract MainActivityContract.View exampleFragment(MainActivity view);

    @Binds
    abstract MainActivityContract.Presenter exampleFragmentPresenter(MainActivityPresenter presenter);
}

活动代码

public class MainActivity extends DaggerAppCompatActivity implements MainActivityContract.View {
    @Inject
    MainActivityContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPresenter.attach();
    }

    @Override
    public void setText(String text) {
        ((TextView) findViewById(R.id.example_text)).setText(text);
    }
}

演示者代码

public class MainActivityPresenter implements MainActivityContract.Presenter {

    private MainActivityContract.View mView;

    @Inject
    public MainActivityPresenter(MainActivityContract.View view) {
        mView = view;
    }

    public void attach() {
        this.mView.setText("Hello world!");
    }
}

通常的dagger-android设置:

申请代码

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

AppComponent

@Component(modules = {ActivityBindingModule.class, AndroidSupportInjectionModule.class})
public interface AppComponent extends AndroidInjector<MyApplication> {
    @Component.Builder
    interface Builder {
        @BindsInstance
        AppComponent.Builder application(Application application);

        AppComponent build();
    }
}

活动绑定模块

@Module
public abstract class ActivityBindingModule {
    @ActivityScoped
    @ContributesAndroidInjector(modules = MainActivityModule.class)
    abstract MainActivity mainActivity();
}

现在我想将活动转换为片段。

重复上述模式,片段代码唯一不同:

public class ExampleFragment extends DaggerFragment implements ExampleFragmentContract.View {
    @Inject
    ExampleFragmentContract.Presenter mPresenter;

    @Inject
    public ExampleFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.example_fragment, container, false);
    }

    @Override
    public void setText(String text) {
        ((TextView) getView().findViewById(R.id.example_text)).setText(text);
    }
}

主要活动被愚蠢,主持人被放弃:

public class MainActivity extends DaggerAppCompatActivity {
    @Inject
    ExampleFragment exampleFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.id.root, exampleFragment);
    }
}

模块仍然存在,但现在注入片段:

@Module
public abstract class MainActivityModule {
    @FragmentScoped
    @ContributesAndroidInjector(modules = ExampleFragmentModule.class)
    abstract ExampleFragment exampleFragment();
}

编译现在失败并显示错误:

Error:(13, 8) error: [dagger.android.AndroidInjector.inject(T)] com.example.michael.daggerfragmentexample.ui.ExampleFragment.ExampleFragmentContract.Presenter cannot be provided without an @Provides-annotated method.
com.example.michael.daggerfragmentexample.ui.ExampleFragment.ExampleFragmentContract.Presenter is injected at
com.example.michael.daggerfragmentexample.ui.ExampleFragment.ExampleFragment.mPresenter
com.example.michael.daggerfragmentexample.ui.ExampleFragment.ExampleFragment is injected at
com.example.michael.daggerfragmentexample.ui.MainActivity.MainActivity.exampleFragment
com.example.michael.daggerfragmentexample.ui.MainActivity.MainActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
A binding with matching key exists in component: com.example.michael.daggerfragmentexample.ui.MainActivity.MainActivityModule_ExampleFragment.ExampleFragmentSubcomponent

我看到它的方式,我有两个级别的深子组件和匕首忽略更深层次的模块。这是我不能做的事情吗?

Link to the full sample code

1 个答案:

答案 0 :(得分:1)

在您的ExampleFragmentPresenter中,您错过了范围

@FragmentScoped
public class ExampleFragmentPresenter implements ExampleFragmentContract.Presenter

此外,您正尝试将片段范围注入添加到MainActivity的活动范围

@Inject
ExampleFragment exampleFragment;

这是错误的,因为您的活动范围对您的片段注入一无所知,所以另外这样做:

public class MainActivity extends DaggerAppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.id.root, ExampleFragment.newInstance());
    }
}

// removed @Inject here
public ExampleFragment() {

}