https://kotlinlang.org logo
Title
r

Rick Regan

12/20/2021, 2:23 AM
Can an app that’s been in the background for a while ultimately be killed off and not restarted even if it has saved instance state?  I recently added saved instance state to my app (
RememberSaveable
and explicit writes to the bundle in
onSaveInstanceState()
) and it restores except sometimes after long stretches in the background (8-12 hours) on my real device. (Some other apps on my device appear to behave similarly, and others don’t, which makes me more curious.)
a

Adam Powell

12/20/2021, 2:25 AM
it's not standard behavior but iirc android cts doesn't test for this. What device are you observing this on?
r

Rick Regan

12/20/2021, 2:25 AM
Pixel 4A
I tested with “Don’t keep activities” on the emulator, and it definitely works to some degree on my real device because before I added support it wouldn’t take long in the background for the app to lose its current state.
Also, if I put the app in the background while navigated to a subscreen the same thing can happen (restarts at home screen eventually). So this seems to be independent of my own handling of saved instance state.
Android 12 BTW
a

Adam Powell

12/20/2021, 2:40 AM
"don't keep activities" is a different behavior entirely, while it can help as a tool to ensure you've implemented some lifecycle code correctly, it's not how real devices behave
r

Rick Regan

12/20/2021, 2:52 AM
So if what I'm seeing is not standard behavior should I write this up as an issue? (Assuming I can do it with a minimal repro...I would write a little app that uses Navigation Compose with two screens, navigate to the second screen and let the app sit in the background. I assume the app is always suppose to restore in the second screen if this is working as expected.)
This is the little test app I am now running on my 4a; I have it navigated to the subscreen and in the background. I will let you know what happens to it (I am expecting that after a while it will reopen on the home screen).
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()
            NavHost(navController = navController, startDestination = "Home") {
                composable(
                    route = "Home",
                ) {
                    Column {
                        Text("Home Screen")
                        Button(onClick = { navController.navigate("Subscreen") }
                        ) {
                            Text(text = "Click to go to Subscreen")
                        }

                    }
                }
                composable(
                    route = "Subscreen",
                ) {
                    Text("Subscreen")
                }
            }
        }
    }
}
Overnight the app disappeared from Recents, and when I reopened it from its icon it started in its ”cold start” state. This does not seem to be a Compose issue though, since other apps on my phone do the same. I haven’t really found the pattern except it seems to only happen after long periods of not using the phone (which usually means only overnight).  Some apps survive, not necessarily the same ones from night to night, and I don't have to have a lot of apps open. I'll try to write something up and submit it. Thanks.
Issue I submitted: "Recent apps disappear and are not restored from saved instance state" https://issuetracker.google.com/u/1/issues/211580605