-
-
Save codeforfun-jp/263eede483a13690fd511cb82ecb3a9f to your computer and use it in GitHub Desktop.
CTB 8-5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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