Search code examples
javaandroidsharedpreferences

How to get Preferences in Android?


I don't understand how preferences work.

I have a preferences activity and one field on it:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
    android:key = "@string/pr_rest_service_url"
    android:title = "@string/rest_service_url"
    android:summary = "%s"
    android:shouldDisableView="false"
    android:selectable="true"
    android:enabled="true"
    android:defaultValue="543543543"
    />
</PreferenceScreen>

In preference activity i use this code to edit it:

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    String editTextPrefKey = getString(R.string.pr_rest_service_url);
    EditTextPreference restUrlTExtEdit = (EditTextPreference) findPreference(editTextPrefKey);
    restUrlTExtEdit.getEditText().setInputType(InputType.TYPE_CLASS_TEXT);
    restUrlTExtEdit.setSummary(restUrlTExtEdit.getText());

    restUrlTExtEdit.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            preference.setSummary(newValue.toString());
            return true;
        }
    });

    Preference saveButton = findPreference(getString(R.string.preference_save_button));
    saveButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            finish();
            return true;
        }
    });
}
}

And its works. Works nice.
Now i want to get preferense from another parts of application. I put this code in MainActivity:

String key = this.getString(R.string.pr_rest_service_url);
SharedPreferences mSharedPreferences = getSharedPreferences(key, Context.MODE_PRIVATE);
String g = mSharedPreferences.getString(key, null);

And got null. So how gonna work with preferences?
Or i made mistake on ths start and should not use PreferenceActivity as settings screen? Or preferenses binded to activity? So how can i get

UPDATE

I tried

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String name = sp.getString("myKey", "");

But got empty string.


Solution

  • Update: Use this then:

    SharedPreferences yourOutputdata =
    context.getSharedPreferences("YourKEY", MODE_PRIVATE); // Save as YourKEY in the Activity which you're passing data to this one ...
    

    Because of this:

    String key = this.getString(R.string.pr_rest_service_url);
    

    You are actually getting string from resource file and saving it to preference which I believe this causes the null.

    Try this:

    To save:

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("key","your text-data here");
    editor.apply();
    

    And to get:

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    String name = sp.getString("key", "");