1

我的问题是:如果我打电话:

PreferenceManager.getDefaultSharedPreferences(getApplicationContext())

在我的应用程序中有两个不同Activities的地方,它是指同一个SharedPreferences文件吗?

更具体地说,我rememberMeSharedPreferences. 我可以从我的应用程序中的两个位置进行更改。看起来当我在一个位置更改它时,它不会在第二个位置生效。

编辑: 我有这个代码:

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)中,我可以看到这个更改也没有生效。

4

3 回答 3

2

使用它来获取 SharedPreferences(myPrefs 是您的文件):

为了保存:

SharedPreferences prefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("rememberMe", yourState);
editor.commit();

忘记:

SharedPreferences prefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
boolean state = prefs.getBoolean("rememberMe", state_by_default);
于 2013-05-08T11:20:39.100 回答
1

将评论转换为答案,

你的问题是你没有commit你的偏好。SharedPreference之后才会更新commit

于 2013-05-08T11:47:39.530 回答
0

是的,应用程序上下文的默认共享首选项文件相同。

于 2013-05-08T11:33:50.867 回答