维护Android Activity的数据:onPause、onSaveInstanceState、onRetainNonConfigurationInstance
我有一个应用程序 Activity
,它在 onCreate
中使用 AsyncTask
从服务加载 XML 文件。 XML 被解析为 ArrayList
。当我切换到不同的 Activity,然后返回主 Activity 时,我希望能够识别出该 XML 文件已加载并使用填充的 ArrayList。
持久化 ArrayList 的最佳方法是什么?
onSaveInstanceState
似乎只支持原语,我无法设置实际调用 onRetainNonConfigurationInstance
的情况。因此,在 onCreate
中,每当我切换到该 Activity
时,都会从服务器加载 XML 数据。我已经使 ArrayList 中的模型实现了 Parcelable,那么可以以某种方式使用它吗?
I have an application Activity
that in onCreate
loads an XML file from a service using an AsyncTask
. The XML is parsed into an ArrayList
. When I switch to a different activity and then back to the main activity, I want to be able to recognize that that XML file was already loaded and use the populated ArrayList
.
What is the best way to persist that ArrayList
?
onSaveInstanceState
only seems to support primitives and I've been unable to set up a case where onRetainNonConfigurationInstance
actually gets called. So in onCreate
, the XML data is loaded from the server ever time I switch to that Activity
. I have made the models that are in the ArrayList
implement Parcelable
, so could use that in some way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白你的问题与多项活动有什么关系。例如,如果用户按下 HOME(惊呼!),会发生什么?您的应用程序最终将被关闭。您想从服务器重新加载数据吗?如果答案是“是”,那么您不需要“持久”任何内容,
onSaveInstanceState()
可能就足够了(见下文)。如果答案是“否”,那么您需要重新考虑您的数据模型方法,因此您安排将数据保存在数据库中,定期与您的 Web 服务同步,并且可能转储ArrayList
并将其替换为光标。如果我的 HOME 问题的答案是“是”,那么您可以保留活动的数据成员中的数据,并且,如果它的大小适中,也可以将其存储在
在
。onSaveInstanceState()
中捆绑Bundle
可以保存Parcelable
的ArrayList
。但是,如果数据集很大(例如 100KB 或更多),您可能不想走这条路,应该考虑我上面描述的“否”路径。旋转屏幕。还有其他情况,但方向变化是最容易触发的情况。
然而,这与你的问题无关。
I don't see where your problem has anything to do with multiple activities. What happens if the user presses HOME (gasp!), for example? Your app will eventually be closed. Do you want to reload the data from the server? If the answer is "yes", then you don't need to "persist" anything, and
onSaveInstanceState()
may suffice (see below). If the answer is "no", then you need to rethink your approach to your data model, so you arrange to keep the data in a database, synchronizing with your Web service periodically, and probably dumping theArrayList
and replacing it with aCursor
.If the answer to my HOME question is "yes", then you can just hold onto the data in a data member of your activity, and, if it is modestly sized, also stash it in the
Bundle
inonSaveInstanceState()
. ABundle
can hold anArrayList
ofParcelable
. However, if the data set is large (say, 100KB or more), you probably don't want to go this route and should consider the "no" path I described above.Rotate the screen. There are other scenarios, but orientation changes are the easiest ones to trigger it.
However, it has nothing to do with your problem.
“onSaveInstanceState 似乎仅支持原语”
onSaveInstanceState 支持对象,只要它们被声明为可序列化。
"onSaveInstanceState only seems to support primitives"
onSaveInstanceState supports objects, as long as they are declared serializable.