ulfben

use onResume to update the highscore

Sep 16th, 2021
983
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ulfbenjaminsson.spaceshootersample
  2.  
  3. import android.content.Intent
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.widget.Button
  7. import android.widget.TextView
  8.  
  9. private const val TAG = "MainActivity"
  10. class MainActivity : AppCompatActivity() {
  11.     override fun onCreate(savedInstanceState: Bundle?) {
  12.         super.onCreate(savedInstanceState)
  13.         setContentView(R.layout.activity_main)
  14.         findViewById<Button>(R.id.startGameButton)?.setOnClickListener {
  15.             val intent = Intent(this, GameActivity::class.java)
  16.               startActivity(intent)
  17.         }
  18.     }
  19.  
  20.     override fun onResume() {
  21.         super.onResume()
  22.         val prefs = getSharedPreferences(PREFS, MODE_PRIVATE)
  23.         val longestDistance = prefs.getInt(LONGEST_DIST, 0)
  24.         val highscore = findViewById<TextView>(R.id.highscore)
  25.         highscore.text = "Longest distance: $longestDistance km"
  26.     }
  27. }
RAW Paste Data