将侦听器接口注入OnAttach的父片段

时间:2018-05-19 16:01:04

标签: android android-fragments dagger-2 dagger

我的问题标题是我的错,但我不知道该写些什么。 目前,我正在从SignupBasicChildFragment的onAttach初始化SignupMainFragment中的validateBasicListener,但基本上我想用dagger2注入它但不知道怎么做,我的代码如下,

public class SignupMainFragment extends DaggerFragment implements SignUPContract.View{

    @Inject
    SignUPContract.Presenter signUPPresenter;

   /* This is being initialized from SignupBasicChildFragment onAttach, 
    but   i want it to inject it with Dagger2, but don't know how 
   to do it. */
   public BasicSignUp validateBasicListener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_signup, container, false);
    }

}

这是我的SignupMainFragment模块

public abstract class SignupModule {
@FragmentScoped
@Binds
abstract SignUPContract.Presenter signUpPresenter(SignUPPresenter presenter);

@FragmentScoped
@Binds
abstract SignUPContract.View signUpView(SignupMainFragment loginFragment);

@FragmentChildScoped
@ContributesAndroidInjector(modules = BasicModule.class)
abstract SignupBasicChildFragment signupBasicChildFragment();
}

这是我的另一个片段,其中SignupMainFragment的接口被初始化,

public class SignupBasicChildFragment extends DaggerFragment implements BasicContract.View{

@Inject
BasicContract.Presenter presenter;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    ((SignupMainFragment) getParentFragment()).validateBasicListener = this;  // I want to inject this with dagger 2
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    return inflater.inflate(R.layout.fragment_child_signupbasic, container, false);
 }

 }

上面的模块,

@Module
public abstract class BasicModule {
@FragmentChildScoped
@Binds
abstract BasicContract.Presenter basicPresenter(BasicPresenter presenter);

@FragmentChildScoped
@Binds
abstract BasicContract.View basicView(SignupBasicChildFragment loginFragment);
}

这是我的界面,它将从SignupBasicChildFragment注入SignupMainFragment,

public interface BasicSignUp
{
    void signUpValidateBasic();
}

0 个答案:

没有答案