【发布时间】:2014-08-13 10:48:56
【问题描述】:
我正在使用单例在我的应用程序中存储全局数据。 这部分数据已在线下载,其他已保存在偏好设置中。
public class MySingleton {
private static MySingleton mInstance;
private MySingleton() {
}
public static synchronized MySingleton getInstance() {
if (mInstance == null) {
mInstance = new MySingleton();
}
return mInstance;
}
private City city; // I download the city online
private int userId; // I save the userId in the preferences
public City getCity(){
return city;
}
public void setCity(City city){
this.city = city;
}
public int getUserId(){
return userId;
}
public void setUserId(int userId){
this.userId = userId;
}
我在第一个活动中在线下载了cityObject,然后将它们设置在单例对象上;
我的问题是当应用程序在后台时,操作系统会杀死应用程序和对象城市,然后单例变为空;
我的解决方案是将城市对象保存在首选项中,当我下载第一个活动时,当城市为空时,我从首选项而不是互联网获取它。
这样做是否明智?
【问题讨论】:
标签: android singleton sharedpreferences