Hi, I’m testing StateKeeper and for some reason my...
# mvikotlin
y
Hi, I’m testing StateKeeper and for some reason my state is not surviving process death, any clue ?
Copy code
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val store = Store(stateKeeper())
            Text(text = store.state.number.toString())
        }
    }
}


@Parcelize
data class State(val number: Int) : Parcelable

class Store(stateKeeper: StateKeeper) {
    val state: State = stateKeeper.consume("Register") ?: State(Random.nextInt())

    init {
        stateKeeper.register("Register") { state }
    }
}
a
Hello. Could you please try moving the Store (and the StateKeeper) initialization out of the
setContent {}
block?
And how do you test the process death?
y
Copy code
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val store = Store(stateKeeper())
    setContent {
        Text(text = store.state.number.toString())
    }
}
Same
I’m Terminate Application by clicking on the button in logcat
a
Could you please try to also save something to the Bundle in the activity and verify if it is restored in
savedInstanceState
? The code provided looks correct, however I had some issues with manual testing as well. Sometimes the process is killed completely as you swipe it out manually from the recents.
y
it is a bit strange because I wanted to print logs into logcat to check if lifecycle methods are invoking, but none of them are printed.
I think I see a difference when I’m putting my application into the background and then terminating the app then everything is correct the state is survived because all of these methods are called. But in the case when my application is in Foreground and then I terminate the app none of the lifecycle methods are called either onSavedInstatnceState is called. I don’t know if it is correct behavior, what do you think?
a
I believe the state can only be saved after moving the app to the background. The state saving happens only in that case.
a
I thought to manually test something like process death, you would need to go into developer settings and set the background process limit to 0. Then instead of closing your app, you simply switch to a different app, the system will kill the process, then you switch back to your app. I could be wrong, but I thought just swiping away your app doesn't simulate the same kind of process death you are looking to test.
a
Sure, there are a few ways. The one with the process limit, pressing the stop button in the LogCat tab, and I believe there is an adb command. Also this behavior can be partially simulated by enabling the Don't Keep Activities mode, in which case just the activity is killed when stopped, not the whole process.