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

时间:2016-06-21 06:45:01

标签: java android sharedpreferences

问题

  1. 我创建了一个SharedPreferenceAdapter.java,其中包含一些常用的连接。
  2. 我在SharedPreferenceAdapter中创建了same activity的两个对象,两者都访问了不同的共享偏好文件。让我们说,第一个firstadb打开ABC.xml,第二个打secondadb DEF.xml
  3. firstadbJSONObjectRequest内使用,当Volley响应时会调用它。同时,secondadb被立即调用并执行其任务,并且没有进一步使用。
  4. 当响应到来且firstadb以某种方式完成任务时,当我附加调试器并查看文件名时,它正在访问DEF.xml而不是ABC.xml
  5. 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);
    }
    

    到目前为止找到的WorkAround

    如果我,而不是创建SharedPreferenceAdapter,直接使用SharedPreferences for secondadb,firstadb工作正常。

1 个答案:

答案 0 :(得分:1)

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

只需删除static关键字,一切都应该正常运行。