xamaringoogle-chrome-osandroidappsonchromeos

Saving state when closing android app on chromeos


Our android app saves state before it is killed via (Activity::OnSaveInstanceState). The works well if the app is back grounded before being killed. (which one has to do before explicitly closing an app android) However on ChromeOS, android apps can be killed without back grounding them.

Apps have an 'x' in the top right corner, which closes the app without back grounding it.

android app running on chromeos showing close button

If the app is ended this way, OnSaveInstanceState doesn't appear to run (or perhaps it doesn't have enough time to finish?), and so the app state isn't saved.

Q: Is there away to either:

  1. Disable/hide this 'x' for the android app?
  2. Save state even if the app is ended this way.

Note: android apps on chrome also have 'back arrow' (not shown in this screen shot - top left on the toolbar), which closes the app. State is saved as expected, when app is closed that way.


Solution

  • Don't save state in Activity::OnSaveInstanceState use Activity::OnPause instead.

    After setting up debugging from Visual Studio for the ChromeBook, I confirmed that Activity::OnSaveInstanceState is not being called, when clicking on the 'x', (like it is when one backgrounds an android app, then explicitly close it

    However Activity::OnPause and Activity::OnDestroy are called.

    Despite its name and contrary to lots of advice, it seems that it's not safe to save state in Activity::OnSaveInstanceState

    Moved state saving to Activity::OnPause works for me.