I'm using dagger 2 in project. I provide an Singleton by a Dagger Modul:
@Override
public void onCreate() {
super.onCreate();
// Dagger%COMPONENT_NAME%
mAppComponent = DaggerAppComponent.builder()
// list of modules that are part of this component need to be created here too
.myModule(new MyModule())
.build();
}
public static MyApp from(@NonNull Context context) {
return (MyApp) context.getApplicationContext();
}
I can inject my singleton in MyFragment:
@Inject protected MySingleton mMySingleton;
@Override
public void onCreate(Bundle savedInstanceState) {
MyApp.from(getContext()).getmAppComponent().inject(this);
}
But I can't inject it inside my RecyclerView.Adapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@Inject protected MySingleton mMySingleton;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyApp.from(context).getmAppComponent().inject(this);
}
}
In my MyAdapter is mMySingleton after run line
MyApp.from(context).getmAppComponent().inject(this);
always null.
Can somebody tell me why?
I understand it.
Much thanks