codedawg82
codedawg82

Reputation: 476

SharedPreferences not saved when exiting Application

I have 3 activities in my Gingerbread 2.3.3 application for Android which I'm constructing in Eclipse.

1) MainActivity --- two options, go to LoginActivity or go directly to HomeScreenActivity

2) LoginActivity --- Enter login credentials (aka username and password), validate, and then store in SharedPreferences. After successfully validating, it goes to the HomescreenActivity. Here's what I have for the SharedPreferences section in LoginActivity:

public static final String PREFS_FILE = "MY_PREFS";

SharedPreferences prefs = getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString("USERNAME", getUsername());
                editor.putString("PASSWORD", getPassword());
                editor.commit();

3) HomescreenActivity --- a basic homescreen that shows who's logged in right now in the top right corner in a TextView. For this my onResume() contains:

public static final String PREFS_FILE = "MY_PREFS";

SharedPreferences prefs = getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
TextView name_text = (TextView) findViewById(R.id.name_text_vw);
name_text.setText(prefs.getString("USERNAME", "");

When I use the LoginActivity and login, I properly see the username in the TextView on the HomescreenActivity. However, when I exit the application and do some other stuff to get the activity off the stack, I want to go back to the application, go directly to my HomescreenActivity and see my username logged in already. But this doesn't happen. Does anyone know why? I thought SharedPreferences was the way to store settings and data even after you've left your application. Maybe I'm not using the correct mode -- aka MODE_WORLD_READABLE or MODE_WORLD_WRITABLE?

Upvotes: 2

Views: 3776

Answers (2)

IAmGroot
IAmGroot

Reputation: 13855

There are shared prefs for activities, and shared prefs for apps. Maybe you are using activity prefs.

To save to preferences:

PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MYLABEL",
      "myStringToSave").commit();  

To get a stored preference:

PreferenceManager.getDefaultSharedPreferences(context).getString("MYLABEL",
     "defaultStringIfNothingFound"); 

Where context is your Context.

Upvotes: 4

Simon
Simon

Reputation: 14472

I have to say I don't know the answer to your specific question, I suspect it's because getSharedPreferences is not static and is a method of a Context. Therefore, you have no control over when commit() actually writes to store. However, I recommend a different approach.

I use a static reference in a custom class which extends Application.

public class MyApp extends Application

{ 

    protected static SharedPreferences preferences;

    public static void loadPreferences(Context context) {

       // load your preferences

    }


    public static void savePreferences(Context context) {


      // save your preferences

    } 

You can access them via MyApp.preferences.

Don't forget to add your Application class to the manifest.

Good luck!

Upvotes: 0

Related Questions