in a similar issue
a claim was made that this is a desired behavior:
The dispose method on State should not be called when the app quits.
The behaviour of the following three cases should all be the same:
- Hitting the home button
- Hitting the back button on the first page
- Switching applications in the app switcher
However, as I pointed in
comments to
both issues, the behavior in the first case
will never be the same in general because of how android works:
in the 2nd and 3rd case, the app will generally not be terminated immediately (unless the system is tight on resources). Therefore if a user navigates back to the app via 'recent apps' switcher, the old connection will still be open (unless server closes it due to application specific timeout of client inactivity) and the server will not even notice that a user navigated away and returned again.
However, in the first case, the app is always terminated immediately and all its OS resources released. It may be removed from switcher or not (depending on android version/flavor, but not sure here). If it's not removed and a user navigates back to it, the old state will be already discarded by this time, a new instance of state will be created and a new connection to the server will be established.
Therefore, since the state is always destroyed in the 1st case (but not necessarily in 2nd and 3rd case), I claim that there should be a reliable way to release state's child resources according to general rule of object ownership mentioned before. Since `dispose` method already exists and many devs expect it to be exactly this mechanism, I claim that `dispose` method should be guaranteed to be called on app exit.
If there is a desire to unify behavior in the above 3 cases, then the only way to achieve this is by reliably terminating app in all 3 cases and thus reliably calling `dispose` also. (which personally I think is a terrible idea: I don't want to lose app's state whenever I switch to another for 10 seconds to copy some text).
I welcome others to provide their opinion/insight.
Thanks!