Skip to content

Instantly share code, notes, and snippets.

@vestrel00
Last active April 27, 2018 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vestrel00/8b0a6f00ac742a4eb9d4be69cef37ab8 to your computer and use it in GitHub Desktop.
Save vestrel00/8b0a6f00ac742a4eb9d4be69cef37ab8 to your computer and use it in GitHub Desktop.
A: 3 - ui/common/BaseFragment.java
public abstract class BaseFragment extends Fragment implements HasFragmentInjector {
@Inject
protected Context activityContext;
// Note that this should not be used within a child fragment.
@Inject
@Named(BaseFragmentModule.CHILD_FRAGMENT_MANAGER)
protected FragmentManager childFragmentManager;
@Inject
DispatchingAndroidInjector<Fragment> childFragmentInjector;
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Perform injection here for versions before M as onAttach(*Context*) did not yet exist
// This fixes DaggerFragment issue: https://github.com/google/dagger/issues/777
AndroidInjection.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*)
AndroidInjection.inject(this);
}
super.onAttach(context);
}
@Override
public final AndroidInjector<Fragment> fragmentInjector() {
return childFragmentInjector;
}
protected final void addChildFragment(@IdRes int containerViewId, Fragment fragment) {
childFragmentManager.beginTransaction()
.add(containerViewId, fragment)
.commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment