Profile picture
Hugo Visser @botteaap
, 9 tweets, 2 min read Read on Twitter
OK #AndroidDev we need to talk about saving and state in your apps using onSaveInstanceState and friends. Broadly speaking this mechanism is there to have users return to your app after an interruption in using it and carry on where they left off. 1/9
Behind the scenes, the app might have been killed by Android and whatever you've saved in onSavedInstanceState is returned to you to restore the app to the previous state, as if the was app kept running in the background. Smoke and mirrors basically. 2/9
The trick is to save just enough in the Bundle provided in onSavedInstanceState to make sure your app is able to resume where it left off. As a bonus, view state is already saved for you by the system. Saved state is also provided after config changes (like rotation). 3/9
So I'd argue that mosts apps need this at some point somewhere and often I run into apps where not using this mechanism leads to bad UX or even crashes. Worse, often I hear devs say that "an app doesn't get killed that fast" so they do not need to save any state ever. 4/9
The thing is that you don't know. While it might not be killed "that fast" on your high end developer phone there's absolutely no guarantee that it isn't killed fast when the user isn't interacting with your app. Hello Nexus 5X user here 👋 5/9
Another argument is that "when the user comes back after ages" it doesn't make sense to restore state. Well, again, if I use your app, get a phone call, check something on Google Maps, then return, your app might have already be killed. You just don't know! 6/9
Lately some of the more clever excuses are more like "We use ViewModel and it keeps state on rotation so we don't need to bother with onSaveInstanceState". That's great 🙌 ...as long as your process is alive. ☠️ 7/9
ViewModel is great and it helps with state and things like network calls that need to keep going regardless of a configuration change, but you still need something to restore the state of your ViewModel when the app is restarted by the OS. 8/9
So start by implementing onSaveInstanceState, and once you have that sorted you are in a great starting position for restoring your ViewModel state too. Test this stuff by turning the "Don't keep activities" developer option to see if weird stuff happens. 9/9
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Hugo Visser
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member and get exclusive features!

Premium member ($3.00/month or $30.00/year)

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!