• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

SharedPreferences problem

I'm trying to make an app with accounts, but I'm struggling with SharedPrefences. I made them save the account name, and it works, but only when I kill app once, I can turn off the phone and get it back on, and it's still there, but when I kill the app with saved preferences for 3/4 times, all accounts are gone. What is the problem, I save preferences with onPause, onDestroy, onStop and read with onResume, onCreate, onRestart. Before I had only the onPause and onResume and it worked the same.

Here is the code :



@override
protected void onPause(){

sharedpref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpref.edit();
editor.putString("uzytkownik1", getIntent().getStringExtra("nazwauz"));
editor.putString("uzytkownik2", getIntent().getStringExtra("nazwauz2"));
editor.putString("uzytkownik3", getIntent().getStringExtra("nazwauz3"));
editor.putString("uzytkownik4", getIntent().getStringExtra("nazwauz4"));
editor.putString("haslo1", getIntent().getStringExtra("haslouzy1"));
editor.putString("haslo2", getIntent().getStringExtra("haslouzy2"));
editor.putString("haslo3", getIntent().getStringExtra("haslouzy3"));
editor.putString("haslo4", getIntent().getStringExtra("haslouzy4"));
editor.apply();
super.onPause();
}



onResume:

@override
public void onResume(){

sharedpref = this.getPreferences(Context.MODE_PRIVATE);
if (sharedpref.getString("uzytkownik1", null) != null || sharedpref.getString("uzytkownik2", null) != null || sharedpref.getString("uzytkownik3", null) != null || sharedpref.getString("uzytkownik4", null) != null) {
user1.setText(sharedpref.getString("uzytkownik1", null));
user2.setText(sharedpref.getString("uzytkownik2", null));
user3.setText(sharedpref.getString("uzytkownik3", null));
user4.setText(sharedpref.getString("uzytkownik4", null));
super.onResume();
}
 
I wouldn't use shared preferences to save this data. I'd use the database.
 
Back
Top Bottom