-1

I am creating a game where you tap the image to increase the score before it disappears. I have 3 activities, the first one is to press the start button, the other one is to play the game and see your score while playing it, the last one is to see your score.

My question is, I want to add a high score section to the last activity. How can I implement that?

Here is my code to increase the score (it is linked with tapped images):

public void increaseScore(View view) {
    score = score + 1;
    scoreText.setText("Score: " + score);
}

Here is my code to change activity:

public void changeActivity() {
    UserScore = scoreText.getText().toString();
    Intent intent = new Intent(GameActivity.this, FinishActivity.class);
    intent.putExtra("Score", UserScore);
    startActivity(intent);
}
Zoe
  • 23,712
  • 16
  • 99
  • 132

2 Answers2

1

You should use SharedPreference to save the high score locally. This value will get deleted only when you uninstall the app or clear app's data. SharedPreference doc - https://developer.android.com/training/data-storage/shared-preferences

tronku
  • 360
  • 2
  • 10
  • i know that, i used shared preferences to save score too, but when it comes to high score i could not find a way to implement it. – Berke Atalay Dec 23 '20 at 17:01
-1

another way to pass data between screens in java and android studio is by creating an intent in which you can send the data from screen to screen. there are tutorials on youtube for intents if you are interested in learning about them.

though here is a bit I can show you:

Intent intent= new Intent(this, SecondActivity.class); creating an intent. the secondactivity is the name of the class we want to send the data to. and when we want to run the intent we need to put some data then start it:

intent.putExtra("winner",WINNER); - putting in the data(example)

then you need to start the activity so it goes to the target page/screen where you want to send the data. which you did correctly in your code. afterwards you need to get it in the second activity-

Intent intent=getIntent();
if(intent.getIntExtra("winner",-1)==-1) { winner.setText("TIE"); }

it would look something like this then you can do the same think if you want to send the data back over and make sure you use the finish() function each time so the intents don't overflow since so many will be open at the same time and not be doing anything.

Dharman
  • 21,838
  • 18
  • 57
  • 107
Gil Caplan
  • 62
  • 8