-2

I have an app which displays the score on the main screen but this sets to zero every time a new game is started. What I want it to do is store that score and only change if the next score is higher, hence it being the highest score.

At the moment a hitCount method is deployed which a finish() method utilises to show the score from the game activity to the main activity.

The finish() method is below:

public void finish(){

    Intent returnIntent = new Intent();
    returnIntent.putExtra("GAME_SCORE", gameView.getHitCount());
    setResult(RESULT_OK, returnIntent);
    super.finish();

}

In the MainMenu class, the following onActivityResult() method is used to get the score to display on the main screen:

protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
    // Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                tvScore.setText(Integer.toString(scoreFromGame));
                //highScore.setText(Integer.toString(scoreFromGame));
            }
        }   
    }

}

Now, I know I must use something like shared preference to have any chance of storing the score and replacing it with a higher number when achieved but I can't figure out how and where.

As always, any help greatly appreciated.

Thanks

Phil Adams
  • 95
  • 2
  • 16
  • Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – OneCricketeer Apr 18 '17 at 20:48
  • You can access SharedPreferences anywhere you have a Context (like an Activity) – OneCricketeer Apr 18 '17 at 20:49

2 Answers2

1

Put the following into your onCreate method to read the high score.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
// Read the high score from Shared Preference
int mHighScore = sharedPref.getInt(getString(R.string.high_score), 0);

Update your high score using Shared Preference:

protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
// Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                tvScore.setText(Integer.toString(scoreFromGame));
                //highScore.setText(Integer.toString(scoreFromGame));

                // Write the new high score to Shared Preference
                SharedPreferences.Editor editor = sharedPref.edit();
                if (scoreFromGame > mHighScore) {
                    editor.putInt(getString(R.string.high_score), scoreFromGame);
                    editor.commit();
                }
            }
        }   
    }
}
Sky
  • 1,435
  • 1
  • 15
  • 24
0

You should definitively read about SharedPreferences and look at the link given in the comments. If you want a quick solution for now:

MainActivity.java

SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Set highscore text on start
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    int highscore = prefs.getInt(PreferenceHelper.KEY_HIGHSCORE, 0);
    tvHighScore.setText(Integer.toString(highscore));
}

protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
    // Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                tvScore.setText(Integer.toString(scoreFromGame));

                int highscore = prefs.getInt(PreferenceHelper.KEY_HIGHSCORE, 0);  // Get highscore stored in prefs
                if (scoreFromGame > highscore) {
                    // Update text and save new highscore
                    tvHighScore.setText(Integer.toString(scoreFromGame));
                    prefs.edit().putInt(PreferenceHelper.KEY_HIGHSCORE, scoreFromGame).apply();
                }
            }
        }
    }

}

PreferenceHelper.java

public final class PreferenceHelper {

    public static final String KEY_HIGHSCORE = "highscore";

}

You don't have to use the PreferenceHelper class, but it's a good practice.

Nicolas
  • 6,611
  • 3
  • 29
  • 73