I have an app using Dagger2 The component is instantiated in the onCreate of the Application:
@Override
public void onCreate() {
super.onCreate();
mComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
Dagger is managing a "CacheRepository" which is a class storing the data used by the user across the app.
The issue I have is: when the app is killed by the system, the Application is destroyed and the instance of the component is lost. But when I launch the app again, the app tries to restore its previous state which includes getting data from the Cache which has now been reinitialized. So the app crashes as the data are null.
How can I prevent that?
An easy option would be to force the app to restart from scratch when killed by the system but I haven't found any solution to do so.
Another solution is to store the cache in the SharedPreferences (or any kind of storage) but I don't like this solution as most of the data in the cache are temporary and it makes the app more complex.