Question

I successfully injected an object into my activity that takes the current activity's context in the constructor. I'm then trying to inject an object that relies on the objected i just injected. How do i make sure that i'm injecting the injected instance into the the second injected object?

My activity

class MainActivity extends RoboActivity{

    @Inject DataSource dataSource;
    @Inject Customer customer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);

    }
}

The first injected object

class DataSource{

    private Context context;

    @Inject
    public DataSource(Context context){
        this.context = context;
    }
}

This object relies on the previously injected object

class Customer{

    private DataSource datasource;

    @Inject
    public Customer(DataSource datasource){
        this.datasource = datasource;
    }
} 
Was it helpful?

Solution

You should annotate DataSource class as a @ContextSingleton. RoboGuice will then inject the same instance in the scope of that context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top