Skip to content Skip to sidebar Skip to footer

Android Using Dependency Injection On Fragments

I can use Dependency Injection for Realm successful, i can use @Inject into activity without any problem, now i want to use that and inject that into Fragments, since i novice to u

Solution 1:

Just add another target method in your component like,

@Component(modules = ApplicationModule.class)@SingletonpublicinterfaceApplicationComponent {
  voidinject(ActivityRegister target);
  voidinject(ActivityMain target);
  voidinject(SocketServiceProvider target);
  voidinject(MyFragment target);
}

And in your fragment do similar thing what your doing in your activity.

public class MyFragment extends Fragment{
   @inject Realm realm;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    ((MyApplication) getActivity().getApplication()).getComponent().inject(this);
}

Post a Comment for "Android Using Dependency Injection On Fragments"