How to get the link in EditTextPreference and update the link saved in SharedPreferences?

61 views Asked by At

My preferences.xml and My Class in MainActivity.

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <EditTextPreference
            android:key="@string/key"
            android:title="@string/title_preferences"
            android:summary="@string/sub_title_preferences"
            android:dialogTitle="@string/popup_preferences" />

My Class

SharedPreferences preferences = getApplicationContext().getSharedPreferences(getString(R.string.key),0);
        SharedPreferences.Editor edit = preferences.edit();

        public String getfeed() {
            String sh = preferences.getString(getString(R.string.key), "");
            return sh;
        }

        public void setfeed(String rssfeed) {
            edit.putString(getString(R.string.key), rssfeed);
            edit.commit();

OnStart()

url = preferences.getfeed();

But is not updating the link typed.

1

There are 1 answers

0
Sukhendu Sadhukhan On

PreferenceScreen is connected to default shared preference. Any change will be automatically saved to it.

To retrieve this value, or modify from any other point of the app you can call the shared preference as

SharedPreferences preferences = getApplicationContext().getDefaultSharedPreference();

Just change this line in your code and everything will work perfectly.