응용 프로그램이 백그라운드로 이동하면 내 (정적 및 싱글 톤) 개체가 지워집니다. 그래서이 객체들을 Applicaton Context에 저장하려고했습니다. 다음 코드를 사용하고 있습니다.ApplicationContext에 개체 저장
Accounts.create (getApplicationContext())는 계정 인스턴스를 저장하기 위해 한 번 호출됩니다.
Application Context에 개체를 저장하는 것이 가능합니까 (신뢰할 수 있습니까)? 다음 방법이 옳은지 확실하지 않습니다. ...
public class Init extends Application {
private Hashtable<Object, Object> globalStore = new Hashtable<Object, Object>();
public void putToGlobalStore(Object key, Object value) {
globalStore.put(key, value);
}
public Object takeFromGlobalStore(Object key) {
return this.globalStore.get(key);
}
public void removeFromGlobalStore(Object key) {
this.globalStore.remove(key);
}
public boolean containsInGlobalStore(Object key) {
return this.globalStore.containsKey(key);
}
}
public class Accounts {
protected Accounts(String name, Context context) {
Init init = (Init) applicationContext;
init.putToGlobalStore(name, this);
}
private static Init applicationContext;
public static void create(Context context) {
if (context instanceof Application)
applicationContext = (Init) context;
else
applicationContext = (Init) context.getApplicationContext();
if (applicationContext.containsInGlobalStore(GLOBAL_NAME))
Logger.log("Warning " + GLOBAL_NAME
+ " is already created. This will remove all old datas");
new Accounts(GLOBAL_NAME, applicationContext);
}
private static final String GLOBAL_NAME = "accounts";
public static Accounts getInstance() {
try {
return (Accounts) applicationContext
.takeFromGlobalStore(GLOBAL_NAME);
} catch (Exception e) {
Logger.log("GLOBAL_NAME Lost");
return null;
}
}
도와주세요.
응답 해 주셔서 감사합니다. 내가 필요한 것은 그것이 실행될 때 응용 프로그램을 통해 데이터를 저장하는 것입니다. –