Application continues to forget sharedpreferences on restart

I do not know what to do with it

It seems to work fine with Android 3.0 and above, but on Android 2.3.3 every time I run the application, it again asks for a username / password.

I use general settings.

This is how I save the settings:

SharedPreferences preferences = MyApplication.getAppContext().getSharedPreferences("athopbalance", MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("username", username).commit(); editor.putString("password", password).commit(); 

And here is how I read them:

  SharedPreferences preferences = MyApplication.getAppContext().getSharedPreferences("athopbalance", Context.MODE_PRIVATE); String username = preferences.getString("username", ""); String password = preferences.getString("password", ""); 

I also tried to save the settings using this code:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()); SharedPreferences.Editor editor = preferences.edit(); editor.putString("username", username).commit(); editor.putString("password", password).commit(); 

And read them with this code:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()); String username = preferences.getString("username", ""); String password = preferences.getString("password", ""); 

But that doesn't work either.

The problem is that before restarting the application, I see that they still exist. However, as soon as I restart, I get "(empty string) for the username and" "for the password.

Any ideas would be highly appreciated

+8
android sharedpreferences
source share
4 answers

I don’t know exactly what happened, but after restarting the problem with the emulator disappeared.

Sorry to waste your time

0
source share
 public class MyApplication extends Application { private static MyApplication instance; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate() called"); } public MyApplication() { super(); Log.d(TAG, "Constructor called"); instance = this; } public static MyApplication getApplication() { if (instance == null) { instance = new MyApplication(); } return instance; } } 

And use:

 MyApplication.getApplication().getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE) 
+2
source share

Try the following:

  SharedPreferences preferences = context.getSharedPreferences("YOUR_TAG", 0); String username = preferences.getString("username", ""); String password = preferences.getString("password", ""); SharedPreferences.Editor editor = preferences.edit(); editor.putString("username", username).commit(); editor.putString("password", password).commit(); 

where context is the context of your application:

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; 
+1
source share

You have 2 options:

  • Get overall preference over the activity lifecycle.

  • Call .clear before .commit

See my answer:

Android Persistent Checkable Menu in Custom Widget after Android reboot

0
source share

All Articles