Issue
as a new android developer I'm trying to adopt the android mindset. I faced a problem while preserving the state of an activity, because onSavedInstanceState does not seem to work consistently between different versions of Android.
Prior to version 3, onSavedInstanceState would preserve data even when the app got killed by manually swiping it off the list of recent apps. In newer versions that is not the case. An alternative way is using shared preferences, but the lifecycle of the activity seems quite complicated.
I could use the onPause/onResume methods, but the are called every time, even during the activity's creation process. A simple solution would be to use a boolean flag to check whether the onCreate event has ran before, but it doesn't feel ok.
What would you suggest as the optimal strategy for preserving state on all occasions (eg: application being killed manually, application being killed by the OS, activity being covered by another one, etc)?
Thank you in advance.
Solution
Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.
Answered By - MyDogTom
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.