-1

I have a double value which is in the range of 0-1. The new highscore value should be the lesser value of the two. Basically for example: if user A had a previous highscore of 0.6, and he just scored 0.4, the new highscore should be 0.4. Please be descriptive as I'm a beginner, thank you.

EDIT: Sorry I didn't make it descriptive enough, but I want the highscore to be saved and be able to be accessed again. So, if the user exits the app and revisits, it still shows the highscore.

1 Answers1

0

It sounds like you'll want to use Shared Preferences to store the value for later: http://developer.android.com/training/basics/data-storage/shared-preferences.html, and then check this when you go to save it.

Here is sample code so you can call writeHighScore(score), and it will save it if it is lower than the previously saved high score:

void writeHighScore(float score)
{
    if (score < getOldHighScore())
    {
        SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putFloat("highscore", score);
        editor.commit();
    }
}    


float getOldHighScore()
{
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    return sharedPref.getFloat("highscore", 1);
}

I used a float here instead of a double. If you want to store a double with shared preferences, you'll need to convert it to and from a Long, like here: Can't put double SharedPreferences

Community
  • 1
  • 1
vgm
  • 148
  • 1
  • 2
  • 12