ulfben icon

use onResume to update the highscore

ulfben | PRO | 09/16/21 10:06:51 PM UTC | 0 ⭐ | 13276 👁️ | Never ⏰ | []
Kotlin |

950 B

|

None

|

0 👍

/

0 👎

package com.ulfbenjaminsson.spaceshootersample
 
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
 
private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.startGameButton)?.setOnClickListener {
            val intent = Intent(this, GameActivity::class.java)
              startActivity(intent)
        }
    }
 
    override fun onResume() {
        super.onResume()
        val prefs = getSharedPreferences(PREFS, MODE_PRIVATE)
        val longestDistance = prefs.getInt(LONGEST_DIST, 0)
        val highscore = findViewById<TextView>(R.id.highscore)
        highscore.text = "Longest distance: $longestDistance km"
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎