Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created June 9, 2021 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/263eede483a13690fd511cb82ecb3a9f to your computer and use it in GitHub Desktop.
Save codeforfun-jp/263eede483a13690fd511cb82ecb3a9f to your computer and use it in GitHub Desktop.
CTB 8-5
package jp.codeforfun.catchtheball;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView scoreLabel = findViewById(R.id.scoreLabel);
TextView highScoreLabel = findViewById(R.id.highScoreLabel);
int score = getIntent().getIntExtra("SCORE", 0);
scoreLabel.setText(score + "");
SharedPreferences sharedPreferences = getSharedPreferences("GAME_DATA", MODE_PRIVATE);
int highScore = sharedPreferences.getInt("HIGH_SCORE", 0);
if (score > highScore) {
highScoreLabel.setText("High Score : " + score);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("HIGH_SCORE", score);
editor.apply();
} else {
highScoreLabel.setText("High Score : " + highScore);
}
}
public void tryAgain(View view) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment