I have a simple increment app. When you press on the button the number goes up. However i also made the number change to red once it's higher then 5. I was able to get the number to save on rotation however the colour resets back to normal. I'm not sure how i can also save the colour. Any ideas on how i can do this?
// number starts at 0
var num = 0
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.textView)
val add = findViewById<Button>(R.id.button)
add.setOnClickListener {
++num
if(num > 5) {
textView.setTextColor(Color.parseColor("#FF0000"))
}
textView.setText(num.toString())
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("int", num)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
if(savedInstanceState != null) {
num = savedInstanceState.getInt("int")
textView.setText(num.toString())
}
}
}
update this method onRestoreInstanceState
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
if(savedInstanceState != null) {
num = savedInstanceState.getInt("int")
if(num > 5){
textView.setTextColor(Color.parseColor("#FF0000"))
}
textView.setText(num.toString())
}
}