简体   繁体   中英

Unable to inject dependency in fragment using dagger2 in Java Android

I am able to inject dependency in activities and its working fine. but calling the same dependency in fragment is not taking access.

This is what i did.

App.Java

public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector {

    @Inject
    DispatchingAndroidInjector<Activity> activityInjector;

    @Inject
    DispatchingAndroidInjector<Fragment> fragmentInjector;

    private AppComponent mComponent;

    @Override
    public void onCreate() {
    super.onCreate();
    mComponent = DaggerAppComponent.builder().application(this).build();
    mComponent.inject(this);
    }

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
    return activityInjector;
    }

    @Override
    public AndroidInjector<Fragment> fragmentInjector() {
    return fragmentInjector;
    }
}

This is AppComponent.Java

@Singleton
@Component(modules = { AndroidInjectionModule.class, ActivityModule.class, FragmentModule.class})
public interface AppComponent extends AndroidInjector<App> {

    void inject(App app);

    @Component.Builder
    interface Builder {
    @BindsInstance
    Builder application(Application application);
    AppComponent build();
    }
}

This is ActivityModule.Java class

@Module
public abstract class ActivityModule {
    @ContributesAndroidInjector
    abstract MainActivity contributeMainActivityInjector();

    @ContributesAndroidInjector
    abstract MyActivity contributeCampaignActivityInjector();
}

this is FragmentModule.java class

@Module
public abstract class FragmentModule {
    @ContributesAndroidInjector
    abstract MyFragment bindMyFragment();
}

Now in my MyFragment class where i want to inject that dependency. on top i add these lines inside class

@Inject
ImageDownloaderApi imageDownloaderApi;

then to check in onCreateView function i did this to check if my dependency is working like this

boolean injected = imageDownloaderApi == null ? false : true;
    Log.d(this.getClass().getSimpleName(), "Dependency worked: "+ String.valueOf(injected));

And it is returning me false everytime.

where as same code works in activites. one more thing i am doing in activities is adding this line below onCreate like this

  @Override
protected void onCreate(Bundle savedInstanceState) {
    AndroidInjection.inject(this);    // this line
    super.onCreate(savedInstanceState);

when in trying to add the same line in onCreateView in fragment

AndroidInjection.inject(this);    

it states red line below this. and if i replace this with getActivity. it gave me runtime error.

Please have a look and guide where i am wrong?

Is the process i am doing for fragment is right or not?. as it is clearly working in activities.

Thanks in advance for your time.

I assume you are using Fragment -s from support library. Try to change public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector to public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector, HasSupportFragmentInjector with subsequent providing of appropriate injector.

This is an easy article that makes this topic clear. Also, HasSupportFragmentInjector interface usage is also described here.

when in trying to add the same line in onCreateView in fragment

AndroidInjection.inject(this);

this should be done in onAttach of your fragment, and if you are using the fragment from the support library, as you should, the line would be AndroidSupportInjection.inject(this)

override fun onAttach(context: Context?) {
    AndroidSupportInjection.inject(this)
    super.onAttach(context)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM