Dagger 2 - 将FragmentManager传递给构造函数

时间:2016-07-14 09:56:34

标签: android dependency-injection dagger-2 dagger

我正在研究Dagger 2库并遇到传递参数的问题,这些参数无法从“extends Application”类中获得。

例如,我需要将FragmentManager传递给ViewPager适配器构造函数。 你会怎么做?

所有MyApplication

public class MyApplication extends Application {

private MyAppComponent mMyAppComponent;

@Override
public void onCreate() {
    super.onCreate();

    Context context = getApplicationContext();

    mMyAppComponent= DaggerMyAppComponent.builder()
            .utilityModule(new UtilityModule())
            .locationModule(new LocationModule(context))
            .appModule(new AppModule(context))
            .pagerAdapterModule(new PagerAdapterModule(context)) //this one is problematic
            .build();
}

public MyAppComponent getmMyAppComponent() {
    return mMyAppComponent;
    }
}

MyAppComponent:

@Singleton
@Component(
    modules = {
            UtilityModule.class,
            LocationModule.class,
            PagerAdapterModule.class,
            AppModule.class
    }

public interface MyComponent {
    FragmentManager fragmentManager(); //is it right?

    public void inject(NavigationActivity navigationActivity);
}

的AppModule:

@Module
public class AppModule {
Context context;

public AppModule(Context context) {
    this.context = context;
}

@Provides
@Singleton
Context provideApplicationContext() {
    return context;
    }
}

PagerAdapterModule:

@Module
public class PagerAdapterModule {
Context context;

public PagerAdapterModule(Context context) {
    this.context = context;
}

@Provides
@Singleton TabsPagerAdapter provideTabsPagerAdapter(FragmentManager fragmentManager) {
    return new TabsPagerAdapter(context, fragmentManager);
    }
}

NavigationActivity:

@Inject TabsPagerAdapter mTabsAdapter; //I want this to be initialized 

3 个答案:

答案 0 :(得分:9)

您需要FragmentActivity才能获得FragmentManager,这意味着您无法在Application级别完成,因为它太早了。所以你必须从AppComponent创建一个组件依赖项,让它命名为ActivityComponent,它将看到AppComponent提供的所有依赖项。

@Component(modules = ActivityModule.class, dependencies = MyAppComponent.class)
public interface ActivityComponent {
    void inject(MainActivity mainActivity);
}

@Module
public class ActivityModule {

    FragmentActivity activity;

    public ActivityModule(FragmentActivity activity) {
        this.activity = activity;
    }

    @Provides
    TabsPagerAdapter provideTabsPagerAdapter() {
        return new TabsPagerAdapter(activity, activity.getSupportFragmentManager());
    }
}

现在在onCreate()您的活动中:

public class MainActivity extends AppCompatActivity {

    @Inject
    TabsPagerAdapter tabsPagerAdapter;

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

        ActivityComponent activityComponent = DaggerActivityComponent.builder()
                                                .activityModule(new ActivityModule(this))
                                                .myAppComponent(MyApplication.getMyAppComponent())
                                                .build();

        activityComponent.inject(this);
    }

}

答案 1 :(得分:0)

我有用于viewPagerAdapter和注入的片段的此构造函数

        @Module
public class HomeActivityModule {

    @Provides
    HomeViewPageAdapter provideHomeViewPageAdapter(HomeActivity homeActivity) {
        return new HomeViewPageAdapter(homeActivity.getSupportFragmentManager());
    }
}

并创建此模块

@Inject
HomeViewPageAdapter homeViewPageAdapter;

并提供HomeActivity

{{1}}

最终在HomeActivity中可以像这样注入viewPagerAdapter

{{1}}

答案 2 :(得分:-2)

您可以将FragmentManager传递给PagerAdapterModule:

Context context;
FragmentManager fragmentManager;

public PagerAdapterModule(Context context, FragmentManager fragmentManager) {
    this.context = context;
    this.fragmentManager = fragmentManager;
}