1

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)

Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
  • 1
    Check `@EntryPoint` in [Hilt Doc](https://developer.android.com/training/dependency-injection/hilt-android). – ADM Dec 08 '20 at 04:58
  • 1
    If you got it working you can add an answer below . – ADM Dec 09 '20 at 06:28

1 Answers1

0

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();
            }
    }
Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118