Android - sharedPreference未更改

时间:2014-04-09 06:51:29

标签: android android-intent sharedpreferences preferences preferencescreen

我在PreferenceScreen一个"按钮"点击它后我需要更改Boolean的{​​{1}}值为true,如果在sharedPreference prefLogin中,该值可以跳过我的跳过。但它不起作用!意图是,但是,所有时间都将我从loginactivity重定向到main,原因? "意图主要"因为如果在LoginActivityonCreate

我该如何修复它?并且使用sharedPreference loginActivity只能保留活动

startLogin

和settings.xml

public class NastaveniaActivity extends PreferenceActivity implements Preference.OnPreferenceClickListener 
{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.settings);

    findPreference("prefLogin").setOnPreferenceClickListener(this);
}



public boolean onPreferenceClick(Preference preference) 
{
    String key = preference.getKey();

    if(key.equals("prefLogin"))
    {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPrefs.edit();

        editor.putBoolean("preflogin", true);
        editor.commit();

        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);

        return true;
    }
    return false;
}
}

LoginActivity - <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="@string/pref_login"> <PreferenceScreen android:title="@string/pref_prihlasovanie" android:summary="@string/pref_prihlasovanie_summary" android:key="prefLogin" > </PreferenceScreen> </PreferenceCategory> <PreferenceCategory android:title="@string/pref_update_setting" > <CheckBoxPreference android:defaultValue="true" android:key="prefSendReport" android:summary="@string/pref_send_report_summary" android:title="@string/pref_send_report" /> </PreferenceCategory> </PreferenceScreen> 此处如果点击登录设置后我必须跳过

onCreate

1 个答案:

答案 0 :(得分:0)

试试这个..

if(key.equals("prefLogin"))
{
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("preflogin", true);
editor.commit();
}