Answer the question
In order to leave comments, you need to log in
Why do you need to inject dependencies into an Activity using a Component?
At the very beginning of studying DI and Dagger, questions arise that I cannot find an answer to.
There is a Car class that has two references to Engine and Wheels.
public class Car {
private static final String TAG = "Car";
@Inject Engine engine;
private Wheels wheels;
@Inject
Car(Wheels wheels){
this.wheels = wheels;
}
public void drive(){
Log.d(TAG, "driving...");
}
}
public class Engine {
@Inject
public Engine() {
}
}
public class Wheels {
@Inject
public Wheels() {
}
}
@Component
public interface CarComponent {
void inject(MainActivity mainActivity);
}
public class MainActivity extends AppCompatActivity {
@Inject Car car;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CarComponent component = DaggerCarComponent.create();
component.inject(this);
car.drive();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question