gpt4 book ai didi

java - 在保存 Activity 中打开另一个共享首选项会替换旧的共享首选项

转载 作者:行者123 更新时间:2023-12-01 09:46:48 27 4
gpt4 key购买 nike

问题

  1. 我创建了一个 SharedPreferenceAdapter.java,其中包含一些常用的连接。
  2. 我在同一 Activity 中创建了两个SharedPreferenceAdapter对象,它们都访问不同的共享首选项文件。比方说,第一个是打开 ABC.xmlfirstadb,第二个是打开 DEF.xmlsecondadb
  3. firstadb 在我的 JSONObjectRequest 中使用,并在 Volley 响应时调用。而 secondadb 会立即被调用并执行其任务,并且不再使用。
  4. 当响应到来并且 firstadb 以某种方式执行其任务时,当我附加调试器并查看文件名时,它正在访问 DEF.xml 而不是 ABC .xml.

SharedPreferenceAdapter.java

public class SharedPreferenceAdapter {

static SharedPreferences main;
static SharedPreferences.Editor edit;
String KEY_FIRST_RUN = "first_run";
String KEY_POSTED = "posted";
String KEY_DEFAULT_FILENAME = "ABC";
Context cont;
Map<String, ?> shpf_contents;

// I was earlier using PreferenceManager.getDefaultPreference but later changed to this code just for debugging. I use this constructor for firstadb.
public SharedPreferenceAdapter(Activity act){
main = act.getSharedPreferences(KEY_DEFAULT_FILENAME, 0);
this.cont = act;
}

// I use this one for secondadb
public SharedPreferenceAdapter(Context cont, String AdapterName){
this.cont = cont;
main = cont.getSharedPreferences(AdapterName, 0);
this.shpf_contents = main.getAll();
}

public boolean onFirstRun(){
edit = main.edit();
edit.putBoolean(KEY_FIRST_RUN, false);
return edit.commit();
}

public boolean onPost(String value){
edit = main.edit();
edit.putString(KEY_POSTED, value);
return edit.commit();
}

public boolean onLoggedIn(String val){
return saveData(Keys.usertable.USER_KEY_TOKEN, val);
}

public boolean isFirstRun(){
return main.getBoolean(KEY_FIRST_RUN, true);
}

public String isPosted(){
return main.getString(KEY_POSTED, null);
}

public String getData(String key){
return main.getString(key, null);
}

public String isLoggedIn(){
return main.getString(Keys.usertable.USER_KEY_TOKEN, null);
}

public boolean saveData(String key, String val){
edit = main.edit();
edit.putString(key, val);
return edit.commit();
}

public boolean saveData(String key, int val){
edit = main.edit();
edit.putInt(key, val);
return edit.commit();
}

public boolean saveData(String key, long val){
edit = main.edit();
edit.putLong(key, val);
return edit.commit();
}

public boolean clearData(){
edit = main.edit();
edit.clear();
return edit.commit();
}

public boolean clearPost(){
edit = main.edit();
edit.remove(KEY_POSTED);
return edit.commit();
}

public void logout(){
edit = main.edit();
boolean success = edit.remove(Keys.usertable.USER_KEY_TOKEN).commit();
boolean bf = main.contains(Keys.usertable.USER_KEY_TOKEN);
this.shpf_contents = main.getAll();
DatabaseAdapter dbadb = new DatabaseAdapter(cont);
dbadb.open();
dbadb.delAll(Keys.Property.PROPERTY_TABLE_NAME);
dbadb.close();
}
}

在我的 Activity 中

onStart(){
getData();
}

onResume(){
SharedPreferenceAdapter secondadb = new SharedPreferenceAdapter(ACTIVITY.this, "DEF");
secondadb.clearData();
}

public void getData(){
SharedPreferenceAdapter firstadb = new SharedPreferenceAdapter(ACTIVITY.this);
// Check if net is working
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
verify_getUserObjects(response);
} catch (JSONException ex){
//TODO Something with ex
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

NetworkResponse response = error.networkResponse;

if(response != null && response.data != null){
try{
JSONObject error_obj = new JSONObject(new String(response.data));
verify_getUserProperty(error_obj);
}catch (JSONException ex) {
// TODO Something with ex
}
}
}
}) {

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();

// SOMEHOW, when isLoggedIn is called, firstadb is accessing "DEF.xml" (XML file used by secondadb)
headers.put(KEY_AUTHORIZATION, VALUE_AUTHORIZATION+firstadb.isLoggedIn());
return headers;
}
};

requestQueue.add(request);
}

目前找到的解决方法

如果我不创建 SharedPreferenceAdapter,而是直接将 SharedPreferences 用于 secondaryadb,firstadb 就可以正常工作。

最佳答案

产生问题的原因是您的 SharedPreferencesSharedPreferences.Editor 是静态的,因此,当您实例化新的 SharedPreferenceAdapter.java 时并更改编辑器或共享首选项对象,它将针对所有实例进行更改,并且始终具有由最后一次调用任何构造函数生成的值。

只需删除 static 关键字,一切就应该正常工作。

关于java - 在保存 Activity 中打开另一个共享首选项会替换旧的共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37937268/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com