No Injector Detected for Fragment Dagger 2.11

I have one fragment activity. I try to insert a fragment, but I get "Injector not found for com.tsiro.dogvip.login.signin.SignInFrgmt exception".

ActivityModule:

@Module(includes = BaseActivityModule.class) public abstract class LoginActivityModule { @PerFragment @ContributesAndroidInjector(modules = SignInFragmentModule.class) abstract SignInFrgmt signInFrgmtInjector(); @Binds @PerActivity abstract Activity activity(LoginActivity loginActivity); } 

FragmentModule:

 @Module(includes = BaseFragmentModule.class) public abstract class SignInFragmentModule { @Binds @Named(BaseFragmentModule.FRAGMENT) @PerFragment abstract Fragment fragment(SignInFrgmt signInFrgmt); } 

The fragment class extends BaseFragment, where HasSupportFragmentInjector is implemented.

BaseFragment:

 public abstract class BaseFragment extends Fragment implements HasSupportFragmentInjector, Lifecycle.View { @Inject DispatchingAndroidInjector<Fragment> fragmentInjector; public abstract Lifecycle.ViewModel getViewModel(); @SuppressWarnings("deprecation") @Override public void onAttach(Activity activity) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // Perform injection here before M, L (API 22) and below because onAttach(Context) // is not yet available at L. AndroidSupportInjection.inject(this); } super.onAttach(activity); } @Override public void onAttach(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Perform injection here for M (API 23) due to deprecation of onAttach(Activity). AndroidSupportInjection.inject(this); } super.onAttach(context); } @Override public void onStart() { super.onStart(); getViewModel().onViewAttached(this); } @Override public void onResume() { super.onResume(); getViewModel().onViewResumed(); } @Override public void onPause() { super.onPause(); } @Override public void onStop() { super.onStop(); getViewModel().onViewDetached(); } @Override public AndroidInjector<Fragment> supportFragmentInjector() { return fragmentInjector; } } 

Can someone tell me what I am missing?

+5
source share
2 answers

The problem with your code is the incorrect implementation of the HasSupportFragmentInjector or HasFragmentInjector (it depends on whether you use the support library for fragments or not).

You must implement this interface either in your activity in which the fragment is hosted, or in your Application class. Personally, I would recommend the following class of the application so that you do not bother to implement it in all actions that host the fragment:

 public class MyApplication extends Application implements HasActivityInjector, HasSupportFragmentInjector { @Inject DispatchingAndroidInjector<Activity> mActivityInjector; @Inject DispatchingAndroidInjector<Fragment> mFragmentInjector; @Override public void onCreate() { super.onCreate(); //The way you build your top-level Application component can vary. This is just an example DaggerApplicationComponent.builder() .application(this) .build() .inject(this); } @Override public AndroidInjector<Activity> activityInjector() { return mActivityInjector; } @Override public AndroidInjector<Fragment> supportFragmentInjector() { return mFragmentInjector; } } 

And then you enter your snippets (as you already do in your code):

 @Override public void onAttach(Context context) { AndroidSupportInjection.inject(this); super.onAttach(context); } 

If the above does not work, be sure to create the appropriate components / subcomponents for the screen you are trying to enter. If you use @ContributesAndroidInjector instead of manually defining components, make sure you have one entry for your screen in your binding module:

@ContributesAndroidInjector(modules = MyScreenModule.class) abstract MyScreenActivity bindMyScreen();

If you still can not get it to work. I recommend reading the actual Dagger Documentation :

Hope this helps.

+4
source

I fixed this error. First, expand the fragment using DaggerFragment

 class BaseFragment : DaggerFragment(){} 

After that, add the onAttach method to the same snippet:

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

PS: I use Kotlin.

0
source

Source: https://habr.com/ru/post/1272752/


All Articles