Last active
April 27, 2018 17:15
-
-
Save vestrel00/8b0a6f00ac742a4eb9d4be69cef37ab8 to your computer and use it in GitHub Desktop.
A: 3 - ui/common/BaseFragment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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