Android 共享首选项覆盖提交()或应用()之前每个键上的最后一个值
我试图在两个不同的键上保存两个布尔值,但每次在键中保存值时,它只会用最后一个值覆盖键 下面是保存值的函数调用Android 共享首选项覆盖提交()或应用()之前每个键上的最后一个值,android,sharedpreferences,key-value,Android,Sharedpreferences,Key Value,我试图在两个不同的键上保存两个布尔值,但每次在键中保存值时,它只会用最后一个值覆盖键 下面是保存值的函数调用 AssignRegistrationFun manage = new AssignRegistrationFun(getApplicationContext()); manage.ChangeDataState(false,true); 下面是检查数据状态的if条件中的函数调用。。。 在这个函数调用中,它返回true,即使我将第一个值设置为false if(manage.che
AssignRegistrationFun manage =
new AssignRegistrationFun(getApplicationContext());
manage.ChangeDataState(false,true);
下面是检查数据状态的if条件中的函数调用。。。
在这个函数调用中,它返回true,即使我将第一个值设置为false
if(manage.checkDataChanged("External"))
下面是包含类详细信息的函数定义
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class AssignRegistrationFun {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME ="Tester";
private static final String EXTERNAL_DATA = "true";
private static final String INTERNAL_DATA = "true";
public AssignRegistrationFun(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void ChangeDataState(boolean EX_state,boolean IN_state){
editor.putBoolean(EXTERNAL_DATA,EX_state);
editor.putBoolean(INTERNAL_DATA,IN_state);
editor.commit();//even editor.apply() not works
}
public boolean checkDataChanged(String type){
if(type.equals("External"))
return pref.getBoolean(EXTERNAL_DATA,false);
else
return pref.getBoolean(INTERNAL_DATA,false);
}
}
请帮帮我,提前谢谢 您的
外部\u数据
和内部\u数据
都设置为相同的字符串值:“true”
,因此当您指定其中一个时,您将覆盖另一个的值
解决方案:使用不同的值,例如:
private static final String EXTERNAL_DATA = "external";
private static final String INTERNAL_DATA = "internal";