This is the 7th day of my participation in the August Text Challenge.More challenges in August

Add the dependent

implementation 'com. Google. Dagger: a dagger: 2.37'
kapt 'com. Google. Dagger: a dagger - compiler: 2.37'
Copy the code

One: Provide examples

One: through the construction method

By adding the @Inject annotation in the constructor of the class

class User @Inject constructor() {}Copy the code

2: Inject via the dagger module

Module injection applies to third-party libraries and is not convenient when adding @Inject annotations to constructors. This is simulated by creating the Student class.

class Student {}Copy the code

@Module
class StudentModule {

    @Provides
    fun provideStudent(a) = Student()
}
Copy the code

Two: provide component containers

@Component
interface ApplicationComponent {
    // The target activity to inject
    fun inject(activity: MainActivity)
}
Copy the code

If you want to add containers (modules), you can add modules arguments to @Component, such as StudentModule above.

@Component(modules = [StudentModule::class])
interface ApplicationComponent {

    fun inject(activity: MainActivity)
}
Copy the code

Three: used in the target Activity

class MainActivity : AppCompatActivity() {
    
    // constructor injection
    @Inject
    lateinit var user: User

    //modules
    @Inject
    lateinit var student: Student

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        DaggerApplicationComponent.create().inject(this)
        Log.i("tag"."onCreate: $user")
        Log.i("tag"."onCreate: $student")}}Copy the code

other

scope

Using scoped annotations, you can limit the life of an object to the life of its components. This means that the same instance is used in scope.

The @Singleton is a default scoped annotation that the Dagger provides, meaning a Singleton that is inside the component container.

Scope annotations are used on the @Inject, @provides, @Sharing, @Module, and @Component annotations to indicate the scope of their role.

Note:

  • If the entity adds a scoped annotation, the component must add the same annotation, or the compilation fails and an error is reported.

For example, add the @Singleton annotation to the provideStudent method in the module’s entity

@Module
class StudentModule {

    @Singleton
    @Provides
    fun provideStudent(a) = Student()
}
Copy the code

The component ApplicationComponent must also add corresponding annotations

// If @singleton is added to module, @singleton must be added to module
@Singleton
@Component(modules = [StudentModule::class])
interface ApplicationComponent {

    fun inject(activity: MainActivity)
    fun inject(activity: SecondActivity)
}
Copy the code

By @singleton, we mean that we keep singletons in the same ApplicationComponent instance.

  • Components add scoped annotations. Entities in a Module do not necessarily need to add scoped annotations. Use default scoped annotations when not added.

  • Singletons are only for the same component. In the example above, modules and ApplicationComponent add @Singleton, which is singletons in MainActivity. If student is also injected in SecondActivity, So that’s another example, because through

DaggerApplicationComponent.create().inject(this)
Copy the code

Injection generates new ApplicationComponent instances, so scoped annotations limit the life of an object to the life of its components.

The component dependence

It is not possible to inject two components into the same Activity.

@Singleton
@Component(modules = [StudentModule::class])
interface ApplicationComponent {

    fun inject(activity: MainActivity)

    fun inject(activity: SecondActivity)
}
Copy the code
@Component
interface UserComponent {
    fun inject(activity: MainActivity)
}
Copy the code

For example, both ApplicationComponent and UserComponent declare that injecting MainActivity will not compile directly.