Android Dagger2如何@提供一种类型和两种不同的实现

Android Dagger2如何@提供一种类型和两种不同的实现,android,dagger-2,Android,Dagger 2,我对Dagger2很陌生,刚刚开始。我想取得这样的成就,但没有成功 这是我的模块 @Module public class UtilModule { @Provides @Named("fragmentUtilActivity") public FragmentUtils providesFragmentUtilForActivity(Context context) { return new FragmentUtils(context);

我对Dagger2很陌生,刚刚开始。我想取得这样的成就,但没有成功

这是我的模块

@Module
public class UtilModule
{
    @Provides
    @Named("fragmentUtilActivity")
    public FragmentUtils providesFragmentUtilForActivity(Context context)
    {
        return new FragmentUtils(context);
    }

    @Provides
    @Named("fragmentUtilFragment")
    FragmentUtils providesFragmentUtilForFragment(Fragment fragment)
    {
        return new FragmentUtils(fragment);
    }

}
这是我的组件

@Component(modules = UtilModule.class)
public interface UtilComponent
{
    @Named("fragmentUtilActivity")
    FragmentUtils fragmentUtilsActivity(Context context);

    @Named("fragmentUtilFragment")
    FragmentUtils fragmentUtilsFragment(Fragment fragment);
}
这是我的
FragmentUtil

package myms.utils;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;

import javax.inject.Inject;

import myms.R;

public class FragmentUtils
{
    private Context context;

    private Fragment hostFragment;

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

    public FragmentUtils(Fragment hostFragment)
    {
        this.hostFragment = hostFragment;
    }

    public void addFragment(Fragment fragment, boolean addToBackStack)
    {
        FragmentTransaction transaction = ((Activity) context).getFragmentManager()
                                                              .beginTransaction();
        transaction.add(R.id.fragment_container, fragment, null);

        if(addToBackStack)
        {
            transaction.addToBackStack(null);
        }

        transaction.commit();
    }

    public void addNestedFragment(Fragment fragment, boolean addToBackStack)
    {
        FragmentTransaction transaction = hostFragment.getChildFragmentManager().beginTransaction();
        transaction.add(R.id.nested_fragment_container, fragment, null);

        if(addToBackStack)
        {
            transaction.addToBackStack(null);
        }

        transaction.commit();
    }

    public void replaceNestedFragment(Fragment fragment, boolean addToBackStack)
    {
        FragmentTransaction transaction = hostFragment.getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.nested_fragment_container, fragment, null);

        if(addToBackStack)
        {
            transaction.addToBackStack(null);
        }

        transaction.commit();
    }
}
我想要的是将fragmentUtils实例与两个不同的实现一起使用,一个用于活动,另一个用于片段。请告诉我我做错了什么

还有人能帮我理解@Component接口中void inject(SomeClass)的用途吗


注意

好吧,经过努力,我可以通过修改UtilMoudle类来解决这个问题

package myms.modules;

import android.app.Fragment;
import android.content.Context;

import javax.inject.Named;

import dagger.Module;
import dagger.Provides;
import myms.utils.FragmentUtils;


@Module
public class UtilModule
{
    private Context context;

    private Fragment fragment;


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

    public UtilModule(Fragment fragment)
    {
        this.fragment = fragment;
    }

    @Provides
    @Named("fragmentUtilActivity")
    public FragmentUtils providesFragmentUtilForActivity(Context context)
    {
        return new FragmentUtils(context);
    }

    @Provides
    @Named("fragmentUtilFragment")
    FragmentUtils providesFragmentUtilForFragment(Fragment fragment)
    {
        return new FragmentUtils(fragment);
    }

    @Provides
    Context provideContext()
    {
        return context;
    }

    @Provides
    Fragment provideFragment()
    {
        return fragment;
    }

}
所以基本上我必须为我的方法提供依赖,比如我的案例中的上下文和片段。最后,为了得到这个实例,我必须这样做。例如活动

UtilComponent component = DaggerUtilComponent.builder()
                                                     .utilModule(new UtilModule(this))
                                                     .build();
        FragmentUtils fragmentUtils = component.fragmentUtilsActivity();
请注意
.utilModule(新的utilModule(this))
,因为这将在构建图形时提供上下文


我对这件匕首很不熟悉,所以请小心使用这种方法。无担保/无适用索赔。快乐匕首:)

有人……?你收到错误吗?请描述您面临的确切问题。
FragmentUtils
看起来应该是两个不同的类:一个用于主机片段,另一个用于活动。您有两组方法,调用错误的方法将导致NullPointerException。这是一个非常明显的迹象,表明此代码应该分为两个不同的类。谢谢David,请查看我下面的答案。那么您想要两个独立的
FragmentUtils
?听起来,使用它们的活动和片段应该都有自己的匕首模块。名称
UtilModule
表明您正在按模块提供的内容类型划分模块。模块应该根据其提供的范围进行划分。