SharedPreferences problem
- By Chester4321
- Android Development
- 1 Replies
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();
}
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();
}
... lost on highway from car bonnet (or hood, if you are from USA), run over by car tyres, but amazingly still works (was in a leather magnetic cover) but with no working screen obviously. Found using Find My Phone. Back cover intact. SmartSwitch installed before accident. At time of this post, the phone is still on, even after a week - thought the battery would have died by now.