RoboGuice getInjector alternate in HILT (Dagger2)

146 Views Asked by At

I am in process of migrating to HILT from RoboGuice in my current app, there are alot of placs where injection in RoboGuice is done using below way:

IHelper helper = RoboGuice.getInjector(getApplicationContext()).getInstance(IHelper.class);

Dose HILT support this kind of injection, as per my knowledge HILT only does field and method injection. And correct me if I am wrong, field injection only works if I have @AndroidEntryPoint annotation declared (that means that class has to be one that aligns with @AndroidEntryPoint approved classes)

1

There are 1 best solutions below

0
Adil Bhatty On

I got it sorted out thanks to @ADM's suggestion, here is how its working for me. Happy to see if any one has any improvement suggestions.

        public class MyDialogFragment extends DialogFragment {
    

            //create interface inside the class where you want to inject
            @EntryPoint
            @InstallIn(ApplicationComponent.class)
            interface MyDialogFragmentEntryPoint {
                    public IHelper helper();
            }

            protected IHelper mHelper

            
            public void anyMethod() {

            //declare object of the Entry Point we declare
            MyDialogFragmentEntryPoint dialogFragmentEntryPoint = EntryPointAccessors.fromApplication(this._context.getApplication(), MyDialogFragmentEntryPoint.class);
            //You can get the instance of helper
            mHelper = dialogFragmentEntryPoint.helper();
            }
    }