Description
What is instance state, and why it exists
On Android, an Activity can be killed at any time by the system. This happens usually when Android needs memory when your Activity is not in the foreground, or because of a non-handled configuration change, such as a locale change.
To avoid the user having to restart what he did from scratch when Android killed the Activity, the system calls onSaveInstanceState(…)
when the Activity is paused, where the app is supposed to save it's data in a Bundle
, and passes the saved bundle in both onCreate(…)
and onRestoreInstanceState(…)
when the task is resumed if the activity has been killed by the system.
The issue about it in flutter
In the flutter apps I tried (Flutter Gallery, and the base project with the FAB tap counter), if I open enough apps to make Android kill the flutter app's Activity, all the state is lost when I come back to the flutter activity (while not having remove the task from recents).
Steps to Reproduce
- Install Flutter Gallery
- Open the device's settings, and in developer options, switch "Don't keep activities" on. (see the screenshot)
This will allow to simulate when Android kills Activities because it lacks memory and you're not in the foreground. - Open the Flutter Gallery app and go anywhere other than the main screen.
- Go to the launcher by pressing the device's home button.
- Press the overview button and return to Flutter Gallery. Here's the bug.
What's expected: The app is in the same state that where we left off, with untouched UI.
What happens: The activity is restarted from scratch, losing all the UI state, even really really long forms.