在保存活动中打开另一个共享首选项将替换旧的共享首选项



>问题

  1. 我创建了一个包含一些常用连接的SharedPreferenceAdapter.java
  2. 我在same activity中创建了两个SharedPreferenceAdapter对象,它们都访问不同的共享首选项文件。假设,第一个是打开ABC.xmlfirstadb,第二个是secondadb那个DEF.xml
  3. firstadb在我的JSONObjectRequest内部使用,当凌空响应时调用它。同时,secondadb被调用立即执行其任务并且没有进一步的用途。
  4. 当响应来临时,firstadb以某种方式执行任务,当我附加调试器并查看文件名时,它正在访问DEF.xml而不是ABC.xml

共享首选项适配器.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();
}
}

在我的活动中

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 用于 secondadb,则 firstadb 工作正常。

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

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

最新更新