I use Dagger in my app.
As the documentation says, I instatiate it at my Application
class as a private
static
class member, and get it by a getter
where I need it.
private void initDagger() {
appComponent = DaggerAppComponent
.builder()
.appModule(new AppModule(this))
.build();
}
But sometimes when call the getter
from my SplashActivity
(this is my Launcher Activity
) appcomponent
is null, so I get a NullpointerException
when I try to inject SplashActivity
into Dagger.
I can't reproduce this issue, but I can it in Crashlytics.
It maybe happens if the user doesn't close the app, but returns after a long time. In this case I call SplashActivity
again and clear the stack, because I have to reinit some things I can do just here. Maybe that's the point when the appcomponent
will be null.
I can see this issue in just SplashActivity
and on just Android 7.x (and some 6.x).
Now I try to get appcomponent
like a singletone
public static AppComponent initDaggerAndGet(IndexApplication application){
if(appComponent == null){
synchronized (lock){
if(appComponent==null)appComponent = DaggerAppComponent
.builder()
.appModule(new AppModule(application))
.build();
}
}
return appComponent;
}
when I get it at the SplashActivity
and I refactor initDagger()
method like this
private void initDagger() {
initDaggerAndGet(this);
}
As I wrote, I can't reproduce the issue, and I can't find something like this issue at SO. What do you think what can be the problem? Will the refactored initalization solve it? Thanks!