简体   繁体   中英

Where to inject AndroidInjection in custom/util classes using dagger2 in java android

I am using dagger 2. And injecting my dependency in activities and fragments which is working fine.

AndroidInjection.inject(this) for activities inside onCreate .

// for the activity
override fun onCreate(savedInstanceState: Bundle?) {
    AndroidInjection.inject(this)
    super.onCreate(savedInstanceState)
}

AndroidSupportInjection.inject(this) for fragments inside onAttach .

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

Now i want to inject my same dependency in a custom or util class named AsyncTaskHandler . I am not able to access that dependency.

I suppose i have to add AndroidInjection.inject(this) line somewhere. but it does not have any onCreate or onAttach function. So how to get the dependency accessible in my util class or to how to call that dependency.

Thanks in advance.

AndroidInjection is not meant to help with classes that are not subclasses of the Android framework. Instead, you are expected to do one of the following:

  • In your DI-enabled class (Activity, Fragment, etc, or anything they inject), inject AsyncTaskHandler or Provider<AsyncTaskHandler> through an @Inject -annotated constructor or field. This will give you a fully-injected AsyncTaskHandler, and the Provider will give you as many as you may need (including zero). This also allows you to replace AsyncTaskHandler or its factory in different configurations, or to replace the AsyncTaskHandler instance in tests.

  • If AsyncTaskHandler is an implementation detail that you don't need to replace, even in tests, you can call new AsyncTaskHandler . The use of DI doesn't imply that every single class is injected. Of course, if AsyncTaskHandler has a lot of dependencies, you may choose to inject for the sake of convenience and configurability even if you do not intend to replace AsyncTaskHandler itself in any configuration.

  • If you must have AsyncTaskHandler inject itself even when there is no enclosing DI environment, you can craft your own solution: Make sure that AsyncTaskHandler has access to the Application instance, cast it to an interface that allows for injection, and then inject it using a members-injection method . You may also choose to save your Component or a MembersInjector<AsyncTaskHandler> into a static field somewhere, and access that to allow for injecting your AsyncTaskHandler—potentially keeping the static field on AsyncTaskHandler itself. You'll want to do that on Application#onCreate or wherever you create your Component instance.

    The difficulty of procuring an Application instance from an arbitrary class instance without relying on a test-unfriendly static injector, or in ensuring that a static field is populated with a Component or MembersInjector whenever your AsyncTaskHandler needs it, is part of the reason that AndroidInjection is limited in its scope to exclude this type of use-case. You'll need to make sure the rest of your code is structured in such a way that this is safe.

Try something like this: You could inject in the base application or AppModule

@Provides
MyUtilClass provideMyUtilClass(Context context) {
    return new MyUtilClass(context)
}

Following is a dummy util class you're trying to inject:

public class MyUtilClass  {
    private Context context;

    public MyUtilsClass (Context context) {
        this.context = context
   }
   // more code
}

Let me know if this helps

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