Skip to content Skip to sidebar Skip to footer

App Keeps Forgetting Sharedpreferences On Restart

I don't know what to do with it any more Seems to be working fine with android 3.0 and higher, but on android 2.3.3 every time I launch the application it is asking for the usernam

Solution 1:

publicclassMyApplicationextendsApplication {
    privatestatic MyApplication instance;

    @OverridepublicvoidonCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate() called");
    }

    publicMyApplication() {
        super();
        Log.d(TAG, "Constructor called");
        instance = this;
    }


    publicstatic MyApplication getApplication() {
        if (instance == null) {
            instance = newMyApplication();
        }
        return instance;
    }
}

And usage:

MyApplication.getApplication().getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE)

Solution 2:

Try this:

SharedPreferencespreferences= context.getSharedPreferences("YOUR_TAG", 0);
 Stringusername= preferences.getString("username", "");
 Stringpassword= preferences.getString("password", "");


  SharedPreferences.Editoreditor= preferences.edit();
    editor.putString("username", username).commit();
    editor.putString("password", password).commit();

where context is the context of your app:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


        context = this;

Solution 3:

Solution 4:

I don't know exactly what happened, but after I've restarted the emulator issue is gone.

Sorry to waste your time

Post a Comment for "App Keeps Forgetting Sharedpreferences On Restart"