Reputation: 153
I am trying to save two Strings in SharedPreferences. When i first run the app they display as supposed to. When i close the app and re open the app the Strings are no longer set to what they were before the closing of the app and the default String value is used. Why is this? Below are my methods for saving and retrieving the two Strings.
Here are the two Strings
Intent intent = getIntent();
playerName1 = intent.getStringExtra("PLAYER_ONE");
playerName2 = intent.getStringExtra("PLAYER_TWO");
Here is the method to store the Strings
private void savePlayerNames()
{
SharedPreferences sharedPreferences = getSharedPreferences("playerNames", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("playerOne", playerName1);
editor.putString("playerTwo", playerName2);
editor.commit();
}
Here is where they are retrieved
private void displayPlayerNames(int current)
{
SharedPreferences sharedPreferences = getSharedPreferences("playerNames", Context.MODE_PRIVATE);
String playerOne = sharedPreferences.getString("playerOne","Player 1");
String playerTwo = sharedPreferences.getString("playerTwo","Player 2");
if(playerchoice[current].getText().toString().equals(""))
{
if (savedTracker % 2 == 0)
{
playerchoice[current].setText(x);
playerNameDisplay.setText(playerTwo + "'s Go!");
savedTracker++;
}
}
}
Upvotes: 1
Views: 47
Reputation: 1828
Use the following class. This will help you get rid of all the confusing code
public class PlayersData {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private static final String PLAYERS_DATA = "PLAYERS_DATA";
private static final String PLAYERS = "PLAYERS";
public PlayersData(Context context) {
sharedPreferences = context.getSharedPreferences(PLAYERS_DATA, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
public void savePlayers(Players players) {
Gson gson = new Gson();
String json = gson.toJson(players);
editor.putString(PLAYERS, json);
editor.commit();
}
public Players getPlayers() {
Gson gson = new Gson();
String json = sharedPreferences.getString(PLAYERS, null);
return gson.fromJson(json, Players.class);
}
public class Players{
private String one;
private String two;
public Players() {
}
public Players(String one, String two) {
this.one = one;
this.two = two;
}
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
}
}
To get/save data simple initialize class and do operations:
PlayersData playersData = new PlayersData(this); //this is the context
One thing I forgot to mention is that I have used gson lib so add this line to your app level Gradle file and build your Gradle:
implementation 'com.google.code.gson:gson:2.8.5'
Upvotes: 1
Reputation: 13539
Replace
editor.commit();
with
editor.apply();
commit()
writes its preferences out to persistent storage synchronously
, which may failed because of various reasons.
apply(
) writes its changes to the inmemory SharedPreferences immediately but starts an asynchronous
commit to disk.
apply() is completely safe. You can use it without worry as you will get updated value of preferences.
Upvotes: 0