Muneem Habib
Muneem Habib

Reputation: 1104

Preference activity doesn't preserve state

I have preference activity which gets URL from user the issue i am facing is that when I close the app and then run the app again, the URL state is not preserved. What i want is that the user sets URL 1 time and every time app runs using that URL until and unless user doesn't change it.

my prefernce activity XML.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="0dp" android:layout_width="0dp">
    <PreferenceCategory android:title="Authentication">


    </PreferenceCategory>
    <PreferenceCategory android:title="Server URL">

        <EditTextPreference android:key="rootUrl"
                            android:title="Server URL"
                            android:summary="URL to upload and download data"
                >
        </EditTextPreference>

    </PreferenceCategory>


</PreferenceScreen>

preference class:

public class AppSettings extends PreferenceActivity {
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.layout.app_settings);
    }
}

code where i am getting value of root URL

SharedPreferences settings= PreferenceManager.getDefaultSharedPreferences(this);
String s=settings.getString("rootUrl","default");
s=s+"?path=./";
m_urlString=s;
m_root=s;

Upvotes: 0

Views: 108

Answers (1)

allprog
allprog

Reputation: 16790

Add android:persistent="true" to the EditTextPreference and it will be persisted automatically.

<EditTextPreference android:key="rootUrl"
                    android:title="Server URL"
                    android:summary="URL to upload and download data"
                    android:persistent="true"/>

Upvotes: 1

Related Questions