Dagger2 如何@Provide 一种具有两种不同实现的类型

Dagger2 How to @Provide one type with two different implementations

我刚开始接触 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 class

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 实例,一种用于 activity,另一种用于片段。请指导我我做错了什么。

也有人可以帮助我理解 @Component 接口中 void inject(SomeClass) 的用途。

此致

好的,经过努力我可以通过修改我的 UtilMoudle 来解决它 class

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;
    }

}

所以基本上我必须为我的方法提供依赖项,例如在我的案例中是上下文和片段。然后最后得到我必须这样做的实例。例如 activity

UtilComponent component = DaggerUtilComponent.builder()
                                                     .utilModule(new UtilModule(this))
                                                     .build();
        FragmentUtils fragmentUtils = component.fragmentUtilsActivity();

请注意 .utilModule(new UtilModule(this)),因为这将在构建图表时提供上下文。

我对 Dagger 这个东西还很陌生,所以请小心使用这种方法。无保证/无适用索赔。快乐的匕首:)