开发者

Is it considered bad practice to use SharedPreferences for transient application state?

For instance, if I have an app that has several forms that use several activities, and then sends data to a website from the final activity, would it be considered bad practice to store the responses of that form in SharedPreferences and then retrieve the data and clear the SharedPreferences? I suppose I could pass them in a bundle but I would like a user to be able to come back to the app if they were in the mi开发者_如何学Goddle of a session.


If it's not a lot of data. SharedPreferences is good enough. A better option might be to store it as JSON in a file and then load it again when the user comes back.


It's generally better to use a Bundle- Eventually the bundle will disappear as your app's process gets killed, but after that amount of time passes, depending on the app, the possibility exists that your user is opening the app to start a new task, not continue the old one. Also, since bundles aren't persistent memory, reading/writing them is not an IO blocking task.

That said, if you are going to use SharedPreferences, be sure to read/write in an AsyncTask, as it's a blocking IO call. Alternatively, depending on how much data you're storing, you could try writing to both sharedpref and Bundle, and then when you want to resume your activity, check for the bundle first- This would give you the option of speeding up an app resume, but still having persistent storage to fall back on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜