我的问题是:如果我打电话:
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
在我的应用程序中有两个不同Activities
的地方,它是指同一个SharedPreferences
文件吗?
更具体地说,我rememberMe
的SharedPreferences
. 我可以从我的应用程序中的两个位置进行更改。看起来当我在一个位置更改它时,它不会在第二个位置生效。
编辑: 我有这个代码:
public View createSettingsOverlay(){
ViewGroup root = (ViewGroup)findViewById(R.id.absoluteOverlay);
LayoutInflater inflater = getLayoutInflater();
View result = inflater.inflate(R.layout.overlay_baloon_settings, root, false);
((TextView)result.findViewById(R.id.loginText)).setText(application.getCurrentlyLoggedUser());
((TextView)result.findViewById(R.id.passwordText)).setText(application.getCurrenlyLoggedPass());
((TextView)result.findViewById(R.id.loginTimeText)).setText(application.getTimeOfLogin().toString());
((TextView)result.findViewById(R.id.settings_popup_server_url)).setText(application.getCurrentUrl());
//Emil Edit
CheckBox rememberMe = (CheckBox)result.findViewById(R.id.cbRememberMe);
//boolean rememberMePreference = PreferenceManager.getDefaultSharedPreferences(DynamicDataActivity.this).getBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false);
Log.d(TAG, "Remember Me set in the preference is: " + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false));
rememberMe.setChecked(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false));
rememberMe.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "boolean isChecked is: " + isChecked);
if (isChecked)
{
Log.d(TAG, "Remember me checkbox in setting overlay set to True");
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, true);
application.setRememberMeChecked(true);
}
else
{
Log.d(TAG, "Remember me checkbox in setting overlay set to False");
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false);
application.setRememberMeChecked(false);
}
}
});
SharedPreferences
当我选中此弹出窗口中的复选框时,它基本上创建了一个带有设置的rememberMe
弹出窗口,关闭它并再次打开它。由于某种原因,它没有再次检查,在其他Activity
(设置Activity
)中,我可以看到这个更改也没有生效。