Is there a way to programmatically restart Activity keeping its ViewModel (as if it was a system config change)?

52 views Asked by At

We have an Android device that in addition to a built-in display supports external USB displays, so user can attach a USB touch screen.

When that happens we need to move app UI to that newly attached display.

According to the docs there is a way of doing it by starting the activity via an Intent and specifying target display ID:

startActivity(
    Intent(),
    ActivityOptions.makeBasic()
        .setLaunchDisplayId(displayId)
        .toBundle()
)
finish()

And it works indeed. But the downside, unlike a config change, the ViewModel is lost, which breaks user flow.

Is there a way (maybe to craft such an Intent) so that it restarted the Activity, but kept the ViewModel, similarly how system does it for a system config change?

0

There are 0 answers