我想在我的片段中注入依赖,我怎么能用匕首做到这一点。 我正在尝试从MainActivity类中获取activityComponent对象。 但是如果我有多个类使用相同的片段,我需要通过检查实例来键入强制转换。这有什么简单的方法......
public class RepositoryFragment extends Fragment implements
RepositoryContract.View {
@Inject
RepositoryContract.Presenter presenter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_repository, container, false);
// obtain Dagger 2 ActivityComponent from Actiity
ActivityComponent activityComponent = ((MainActivity)getActivity()).getActivityComponent();
// Inject dependencies
activityComponent.inject(this);
presenter.setView(this);
...
return view;
}
}