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