Saving data with Shared Preferences - increment saved value a save it again

38 Views Asked by At

maybe it's not so clear from the title, but I dont't know how else to put it. Imagine you have a quiz. You finished it with some amount of wrong and correct answers (e.g.: total questions 20, wrong answers 5, correct 15). Now I save it with SharedPrefs to display it in another activity (fragment in this case), where are the overall stats.

public void saveData(){
quizData = getSharedPreferences("Quiz Stats", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = quizData.edit();
editor.putInt("Total questions", questionsLength);
editor.putInt("Correct Answers", correctAnswerCount);
editor.putInt("Wrong Answers", wrongAnswerCount);
editor.apply();
}

Now I get it in Stats Fragment a display it in some TVs:

public void setData() {
sharedPreferences = this.getActivity().getSharedPreferences("Quiz Stats",Context.MODE_PRIVATE);
totalQueNum = sharedPreferences.getInt("Total questions",0);
correctAnswersNum = sharedPreferences.getInt("Correct Answers", 0);
wrongAnswersNum = sharedPreferences.getInt("Wrong Answers", 0);
}

Int's here are as public static. This works nicely, each time you play other quiz, it overwritten with new values. I need to increment it. So if your last played quiz was: Total questions: 20, Correct answers: 15, Wrong answers: 5,

And you decide to play other one, where you score: Total questions: 20, Correct answers: 18, Wrong answers: 2

Then in Stats should be: Total questions: 40, Correct answers: 33, Wrong answers: 7

And so on. The results should stack. How to achieve this? I've tried to create another int for incoming new changes. Something like this:

 incomingQueNum = sharedPreferences.getInt("Total questions", 0);
 incomingCorrectNum = sharedPreferences.getInt("Correct Answers", 0);
 incomingWrongNum = sharedPreferences.getInt("Wrong Answers", 0);
 totalQueNum = totalQueNum + incomingQueNum;
 correctAnswersNum = correctAnswersNum + incomingCorrectNum;
 wrongAnswersNum = wrongAnswersNum + incomingWrongNum;

This works, but incoming variables stay saved till another quiz is played which means that each time you switch to another fragment and back to Stats fragment, it's incremented again. I'm missing something obvious, I know that, just don't know what. Also any advice for improvements of my code are welcomed. Thanks!

Edit: I've tried to do something like this, which works fine when app runs. Data is saved to Stats fragment each time and it's incremented only when another quiz is played.

    //Incoming new data
    incomingQueNum = sharedPreferences.getInt("Total questions", 0);
    incomingCorrectNum = sharedPreferences.getInt("Correct Answers", 0);
    incomingWrongNum = sharedPreferences.getInt("Wrong Answers", 0);
    //Incrementing new data
    totalQueNum = totalQueNum + incomingQueNum;
    correctAnswersNum = correctAnswersNum + incomingCorrectNum;
    wrongAnswersNum = wrongAnswersNum + incomingWrongNum;
    //Saving new data
    editor.putInt("Total Stats questions", totalQueNum);
    editor.putInt("Total Stats Correct Answers", correctAnswersNum);
    editor.putInt("Total Stats Wrong Answers", wrongAnswersNum);
    editor.apply();
    //Deleting incoming data
    sharedPreferences.edit().remove("Total questions").apply();
    sharedPreferences.edit().remove("Correct Answers").apply();
    sharedPreferences.edit().remove("Wrong Answers").apply();

But after app is closed, and run again, everything is zero - no data saved, don't know why. Any ideas?

0

There are 0 best solutions below