@Scope
to create objects in a single copy (singleton) . @Named
to separate methods that provide objects of the same type. @Qualifier
as an alternative to @Named
.Component
Activity
level? Activity
is created and destroyed in its life cycle, and what happens to dependencies? Dependencies created inside the Activity
destroyed with the Activity
.MainActivity
as a separate object and create our own module and component for it.Dagger2Part2
branch .Activity
Level AreaMainActivityFeature
.MainActivity
. @Scope public @interface MainActivityScope {}
MainActivity
MainActivity
and mark it with the annotation just created. @Component(dependencies = RandomUserComponent.class) @MainActivityScope public interface MainActivityComponent { RandomUserAdapter getRandomUserAdapter(); RandomUsersApi getRandomUserService(); }
MainActivityComponent
to refer to the RandomUserComponent
using the dependencies
attribute. In other words, this attribute tells Dagger 2 to access the RandomUserComponent
if additional dependencies are required.Activity
level, we need an adapter for the API and an object to create calls to the RandomUsersAPI
. Therefore, we implement the getRandomUserAdapter()
and getRandomUserService()
methods. MainActivity
@Module public class MainActivityModule { private final MainActivity mainActivity; public MainActivityModule(MainActivity mainActivity) { this.mainActivity = mainActivity; } @Provides @MainActivityScope public RandomUserAdapter randomUserAdapter(Picasso picasso){ return new RandomUserAdapter(mainActivity, picasso); } }
MainActivity
via an adapter is not necessary, I did this for an example. If you need a context for Picasso
, then you can use holder.imageView.getContext()
.@MainActivityScope
annotation, which is added to the randomUserAdapter()
method to limit the scope of dependency use to the Activity
level.modules
attribute. @Component(modules = MainActivityModule.class, dependencies = RandomUserComponent.class) @MainActivityScope public interface MainActivityComponent { RandomUserAdapter getRandomUserAdapter(); RandomUsersApi getRandomUserService(); }
Application
Class public class RandomUserApplication extends Application { // private RandomUserComponent randomUserApplicationComponent; public static RandomUserApplication get(Activity activity){ return (RandomUserApplication) activity.getApplication(); } @Override public void onCreate() { super.onCreate(); Timber.plant(new Timber.DebugTree()); randomUserApplicationComponent = DaggerRandomUserComponent.builder() .contextModule(new ContextModule(this)) .build(); } public RandomUserComponent getRandomUserApplicationComponent(){ return randomUserApplicationComponent; }
Application
, contains all application-level dependencies — RandomUserApplicationComponent
.MainActivity
DaggerMainActivityComponent
class for you. To use Activity
level dependencies, we need to get some application level dependencies. public class MainActivity extends AppCompatActivity { ... @Override protected void onCreate(Bundle savedInstanceState) { .... MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder() .mainActivityModule(new MainActivityModule(this)) .randomUserComponent(RandomUserApplication.get(this).getRandomUserApplicationComponent()) .build(); randomUsersApi = mainActivityComponent.getRandomUserService(); mAdapter = mainActivityComponent.getRandomUserAdapter(); .... } }
afterActivityLevelComponent()
look at the afterActivityLevelComponent()
method in the project branch.Activity
. Congratulate yourself.randomUserApi = mainActivityComponent.getRandomUserService();
mAdapter = mainActivityComponent.getRandomUserAdapter();
…
@Inject
annotationRandomUserService
and a RandomUserAdapter
, let Dagger 2 handle the field, which we will mark with the @Inject annotation.@Inject
annotation as soon as possible. You can view the full example in the next thread .MainActivityComponent
getRandomUserService()
and getRandomUserAdapter()
methods and add a method for implementing MainActivity
. @Component(modules = MainActivityModule.class, dependencies = RandomUserComponent.class) @MainActivityScope public interface MainActivityComponent { void injectMainActivity(MainActivity mainActivity); }
MainActivity
public class MainActivity extends AppCompatActivity { .... @Inject RandomUsersApi randomUsersApi; @Inject RandomUserAdapter mAdapter; .... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ..... MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder() .mainActivityModule(new MainActivityModule(this)) .randomUserComponent(RandomUserApplication.get(this).getRandomUserApplicationComponent()) .build(); mainActivityComponent.injectMainActivity(this); .... } }
void
), it realizes that there must be something that it needs in the class, that is, it will initialize the fields in the class that are marked with the @Inject
annotation.Activity
level. We also saw an example of using the @Inject
annotation.Source: https://habr.com/ru/post/345898/
All Articles