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

SharedPreferences problem

Chester4321

Lurker
Jun 22, 2019
1
0
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();
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones