Hi,
I read the doc about when injecting an Application vs a Context vs an Activity, but I still have some questions.
In a custom adapter, I need to use Picasso to fetch images but it requires a Context.
I've been using different approaches, but not sure which one is the best.
1) Inject Context
public class MyAdapter extends BaseAdapter {
@Inject private Context context;
public View getView() {
// Use context
}
}
2) Pass an Activity
public class MyAdapter extends BaseAdapter {
private Context context;
@Inject MyAdapter(@Assisted Context context) {
this.context = context;
}
}
Factory to build MyAdapter:
public interface AdapterFactory {
MyAdapter createMyAdapter(Context context)
}
And use MyAdapter:
listView.setAdapter(factory.createMyAdapter(getActivity()));
So my questions are:
1) when injecting a Context as class member, do I need to worry about memory leaks?
2) when passing a Context, should I use Assisted Injection, or can I let RoboGuice build it for me in the constructor?
Same if I need a Resources.
3) when passing a Context (assisted or not), do I need to save it as weak reference?
public class MyAdapter extends BaseAdapter {
private WeakReference<Context> context;
@Inject MyAdapter(Context context) {
this.context = new WeakReference<Context>(context);
}
}
4) in my scenario, it seems that I need an Activity as Context, but I'm not entirely sure... In which situation should I inject an Activity vs a Context?
And by injecting an Activity, it means a generic one, or the active one (like MainActivity, LoginActivity etc)?
Thanks for your help!