-
Notifications
You must be signed in to change notification settings - Fork 1
Dagger2
Injecting Fragment objects commit
-
its host Activity extends
DaggerAppCompatActivity
; -
it extends
DaggerFragment
; -
Use
@Inject
to annotate fields of this fragment that REQUIRE dependency, then delete relates code that create the fields instance; -
Use
@Inject
to annotate constructors that PROVIDE dependency, or -
Create a
@Module
annotated class, and within this class use an@Provides
annotated method to provide a dependency. -
Use
ContributesAndroidInjector
to install module to dagger graph, because the fragment must be hosted by one activity, that means the activity should also be added to dagger graph:This annotation must be applied to an abstract method in a {@link dagger.Module} that returns a concrete Android framework type (e.g. Activity, Fragment, Service, etc). The method should have no parameters.
/** * ArticleListFragment is hosted by ArticleListActivity, so we need to * install the fragment's module to this activity. */ @ContributesAndroidInjector(modules = { ArticleListActivityHostedFragment.class }) abstract ArticleListActivity bindArticleListActivity(); /** * The reason to create this class is that this activity maybe host many fragments. */ @Module abstract class ArticleListActivityHostedFragment { @ContributesAndroidInjector(modules = ArticleListFragmentModule.class) abstract ArticleListFragment bindArticleListFragment(); // another fragment ... }
- Interfaces can’t be constructed.
- Third-party classes can’t be annotated.
- Configurable objects must be configured!
Add @Inject to constructor to provide dependency, instead of defining an unnecessary @Provides annotation commit
Because @Inject
-annotated constructor works here !
@Module
public class ArticleListFragmentModule {
ArticleListFragmentViewModel provideArticleListFragmentViewModel(ArticleListFragment fragment, ArticleListViewModelFactory factory) {
return ViewModelProviders.of(fragment, factory).get(ArticleListFragmentViewModel.class);
}
- @Provides
- ArticleListViewModelFactory provideArticleListViewModelFactory(Context context, DemoRepository repository) {
- return new ArticleListViewModelFactory(repository);
- }
}
public class ArticleListViewModelFactory extends ViewModelProvider.NewInstanceFactory {
private final DemoRepository mRepository;
+ @Inject
public ArticleListViewModelFactory(DemoRepository repository) {
mRepository = repository;
}
The following 3 articles introduce how to use Dagger2 in a real Android App Project.
In part1, to attach activities/fragments to dagger graph, it create a lot boilerplate classes and needs a lot repetitive tasks, so is too complicated, so just walk through part1.
In part2, use @ContributesAndroidInjector
to simplify dagger graph, check this commit for details
In part3, use DaggerActivity
, DaggerFragment
, DaggerApplication
to reduce boilerplate in your Activity/Fragment/Application, use AndroidInjector<T>
in your dagger components to reduce boilerplate too.