In my app I store some data in the Application
class:
public HashMap<String, HashMap<String, String>> getConfigs() {
if (mConfigs != null) {
return mConfigs;
}
}
public void setConfigs(HashMap<String, HashMap<String, String>> configs) {
mConfigs = configs;
}
Then I call it from an Activity
for further use:
public String getValueByTag(String tag){
return MyApplication.getInstance().getValueByTag(tag);
}
When I press the home button and run it later (i.e. after 1 hour), it crashes with a NullPointerException
.
I know that the Android OS automatically kills the application if it is not used for a while, but what should I do to handle this problem?
Store your data in SharedPreferences or in DB. You can also put them into JSONObject and store them in a form of file in your app folder. You would have to do such serialization each time setConfigs is called, or in your activity onPause. In Application.onCreate, you would restore your mConfigs .