I cannot find the savedInstanceState function shown in code examples in the Restore UI state after activity or process recreation section of the State and Jetpack Compose that the section says "retains state across activity and process recreation."
I did find the androidx.compose.runtime.saveable documentation which contains rememberSaveable which seems to be the renaming or replacement of savedInstanceState and its documentation also says "... the stored value will survive the activity or process recreation."
However, when I use it in my code, the state does not survive the back button, although it does survive rotations. That's contrary to what the documentation says.
package com.example.jetwatch
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
class MainActivity : AppCompatActivity() {
override fun onCreate(bundle: Bundle?) {
super.onCreate(bundle)
setContent {
MaterialTheme(colors = if (isSystemInDarkTheme()) darkColors() else lightColors()) {
Surface {
Column {
Row {
Column {
var m by rememberSaveable { mutableStateOf(0) }
Text("m = $m")
Button(onClick = { ++m }) {
Text("bump")
}
}
}
}
}
}
}
}
}