Dependence injection at fragment using dagger
Step 1 : Add to activity module
@Subcomponent(modules = {ActivityModule.class}) public interface FragmentComponent {
void inject(HamburgerActivity mainActivity);
void inject(HomeScreenFragment homeScreenFragment);
}
Step 2: inject current fragment in onViewCreated
@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.com_philips_lumea_treatment_done, null); } @Overridepublic void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // inject current fragment ((Application) getActivity().getApplication()).getFragmentComponent().inject(this);}Step 3 : Register and Unregister@Overridepublic void onStop() { super.onStop(); if (eventing != null) { eventing.unregister(this); } } @Overridepublic void onStart() { super.onStart(); if (eventing != null && !eventing.isRegistered(this)) { eventing.register(this); } }
Comments
Post a Comment