Answer the question
In order to leave comments, you need to log in
Dagger 2 inject into ContentProvider?
Hello.
I am using dagger 2, I want to inject SqliteOpenHelper into ContentProvider
@Module
public class ApplicationModule {
private Application mApp;
public ApplicationModule(Application app) {
mApp = app;
}
@Provides
@Singleton
Context provideContext() {
return mApp.getApplicationContext();
}
@Provides
@Singleton
DatabaseHelper provideDatabaseHelper() {
return new DatabaseHelper(mApp.getApplicationContext());
}
}
public class App extends Application {
public static App app;
private AppComponent mAppComponent;
@Override
public void onCreate() {
super.onCreate();
Log.d("Application", "onCreate ");
app = this;
initComponent();
}
public static App getInstance(){
return app;
}
ApplicationModule getApplicationModule() {
return new ApplicationModule(this);
}
public AppComponent getAppComponent(){
return mAppComponent;
}
void initComponent() {
mAppComponent = DaggerAppComponent.builder()
.applicationModule(getApplicationModule())
.build();
}
}
@Component(modules = {ApplicationModule.class})
@Singleton
public interface AppComponent {
void inject(FriendsActivity activity);
void inject(Provider provider);
}
@Inject
public DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends_m);
((App) getApplication()).getAppComponent().inject(this);
........
public class Provider extends ContentProvider {
@Inject
DatabaseHelper databaseHelper;
@Override
public boolean onCreate() {
App.getInstance().getAppComponent().inject(this);
return false;
}
.......
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