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

标签 android dagger-2

我刚刚开始接触 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 实例,一种用于 Activity ,另一种用于 fragment 。请指导我我做错了什么。

也有人可以帮助我理解 @Component 接口(interface)中 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;
    }

}

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

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

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

我对 Dagger 这件事还很陌生,所以请谨慎使用这种方法。无保证/无适用 claim 。快乐的 Dagger :)

关于android - Dagger2 如何@Provide 一种具有两种不同实现的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44885960/

相关文章:

android - 无法销毁 Activity 错误

android - Kotlin 错误 : Dagger does not support injection into private fields

android - Dagger 2 : multi-module project, 注入(inject)依赖但在运行时出现 "lateinit property repository has not been initialize"错误

java - Android TLSv1.2 握手失败

android - 如何在 Android 中进行 Realm 迁移?

android - 需要读取固定采样率的android传感器

android - Dagger 2 - 注入(inject)构造函数或提供方法哪个更好?

android - : java. lang.NoSuchMethodError : okhttp3. internal.Platform.log 导致 Retrofit2 登录出错

android - 如何让 Dagger 1 和 Dagger 2 在一个 Android 项目中共存?

java - ProGuard 模拟异常