Android Dependency Injection with Dagger 2 suggest change

Create Module Class and Singleton annotation for Object

import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;

@Module
public class VehicleModule {
 
    @Provides @Singleton
    Motor provideMotor(){
        return new Motor();
    }
 
    @Provides @Singleton
    Vehicle provideVehicle(){
        return new Vehicle(new Motor());
    }
}

Every provider (or method) must have the @Provides annotation and the class must have the @Module annotation. The @Singleton annotation indicates that there will be only one instance of the object.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents