Component - Saving instance state
This component shows how an Android app saves and restores its state when the screen rotates or the app is temporarily stopped. It keeps the user input safe so it doesn't disappear.
This component shows how an Android app saves and restores its state when the screen rotates or the app is temporarily stopped. It keeps the user input safe so it doesn't disappear.
Activity ├── LinearLayout (vertical) │ ├── EditText │ └── Button └── TextView
onSaveInstanceState in Android Kotlin apps?onSaveInstanceState?override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("score", 10)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val score = savedInstanceState?.getInt("score") ?: 0
println(score)
}override fun onSaveInstanceState(outState: Bundle) {
outState.putInt("count", count)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val count = savedInstanceState?.getInt("count") ?: 0
}Pass the test and this pattern moves unknown