Question

I am a newbie in Android and I'm just trying to permanently store a string.

I want to get this string from a PreferenceActivity and then update the text of a TextView.

I was reading about the available options for persistent storage: http://developer.android.com/guide/topics/data/data-storage.html#pref

I've tried to use SharedPreferences but it's very unclear for me on how it should work.

I have created a very simple test app.

MainActivity.java

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

/* method that starts preferences */
public void openPreferences(View v) {
    Intent i = new Intent(this, Preferences.class);
    startActivity(i);
}

@Override
protected void onStart(){
    super.onStart();

    // Get preferences
    SharedPreferences preferences = getPreferences(0);
    String name = preferences.getString("name","Empty string!");

    // Create a RemoteViews layout
    RemoteViews views = new RemoteViews(getPackageName(),R.layout.main);       
    // set new text for labels
    views.setTextViewText(R.string.name, name);
}

@Override
protected void onStop(){
    super.onStop();

    // We need an Editor object to make preference changes.
    // All objects are from android.context.Context
    SharedPreferences preferences = getPreferences(0);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("name", "This is a test");
    // Commit the edits!
    editor.commit();
}
}

Preferences.java

public class Preferences extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
}

AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Preferences" 
              android:label="@string/preferences_title">
    </activity>

</application>

The text of the TextView never changes, it is always set to the value I set on strings.xml.

Can you please help me to understand what am I doing wrong?

Thank you very much.

Was it helpful?

Solution

You're extremely close to what I'd recommend:

1) Define some shared preferences:

    public static final String MY_PREFERENCES = "MyPreferences";
    ...
    public static final SOME_PREFERENCE = "SomePreference";

2) Read from shared preferences

    SharedPreferences myPreferences;
    ...
    myPreferences = getSharedPreferences (MY_PREFERENCES, Context.MODE_PRIVATE);

3) Save/update a shared preferences:

    Editor editor = myPreferences.edit ();
    editor.putString (SOME_PREFERENCE, "abc");
    editor.commit ();

OTHER TIPS


Quick example of saving/writing username, password into preferences.

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

Editor edit = preferences.edit();        
edit.putString("username", "abcd");        
edit.putString("password", "abcd#");        
edit.commit();

Quick example of reading username, password from preferences.

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);    

String strUsername = preferences.getString("username", null);   
String strPassword = preferences.getString("password", null);   

I just realized my code related to SharedPreferences is correct, but the TextView never updates because I tried to edit directly the string itself, not the TextView!

Just resolved this way:

    /* Update text of TextView */
    TextView t = (TextView) this.findViewById(R.id.name);
    // previously was R.string.name - not correct!
    t.setText(name);

Many thanks for your help guys :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top