维护Android Activity的数据:onPause、onSaveInstanceState、onRetainNonConfigurationInstance

发布于 2024-10-19 19:43:54 字数 475 浏览 2 评论 0原文

我有一个应用程序 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒带 2024-10-26 19:43:54

持久化 ArrayList 的最佳方法是什么?

我不明白你的问题与多项活动有什么关系。例如,如果用户按下 HOME(惊呼!),会发生什么?您的应用程序最终将被关闭。您想从服务器重新加载数据吗?如果答案是“是”,那么您不需要“持久”任何内容,onSaveInstanceState() 可能就足够了(见下文)。如果答案是“否”,那么您需要重新考虑您的数据模型方法,因此您安排将数据保存在数据库中,定期与您的 Web 服务同步,并且可能转储 ArrayList并将其替换为光标。

onSaveInstanceState 似乎仅支持原语

如果我的 HOME 问题的答案是“是”,那么您可以保留活动的数据成员中的数据,并且,如果它的大小适中,也可以将其存储在 onSaveInstanceState() 中捆绑Bundle 可以保存 ParcelableArrayList。但是,如果数据集很大(例如 100KB 或更多),您可能不想走这条路,应该考虑我上面描述的“否”路径。

我无法设置实际调用 onRetainNonConfigurationInstance 的情况。

旋转屏幕。还有其他情况,但方向变化是最容易触发的情况。

然而,这与你的问题无关。

What is the best way to persist that ArrayList?

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 the ArrayList and replacing it with a Cursor.

onSaveInstanceState only seems to support primitives

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 in onSaveInstanceState(). A Bundle can hold an ArrayList of Parcelable. 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.

I've been unable to set up a case where onRetainNonConfigurationInstance actually gets called.

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.

幸福不弃 2024-10-26 19:43:54

“onSaveInstanceState 似乎仅支持原语”

onSaveInstanceState 支持对象,只要它们被声明为可序列化。

// ON_SAVE_INSTANCE_STATE
// save instance data (5) on soft kill such as user changing phone orientation
protected void onSaveInstanceState(Bundle outState){
    password= editTextPassword.getText().toString(); 
    try {
        ConfuseTextStateBuilder b= ConfuseTextState.getBuilder();
        b.setIsShowCharCount(isShowCharCount);
        b.setTimeExpire(timeExpire); 
        b.setTimeoutType(timeoutType);
        b.setIsValidKey(isValidKey); 
        b.setPassword(password);
        state= b.build(); // may throw
    }
    catch(InvalidParameterException e){
        Log.d(TAG,"FailedToSaveState",e); // will be stripped out of runtime
    }
    outState.putSerializable("jalcomputing.confusetext.ConfuseTextState", state);  // save non view state
    super.onSaveInstanceState(outState); // save view state
    //Log.d(TAG,"onSaveInstance");
}

"onSaveInstanceState only seems to support primitives"

onSaveInstanceState supports objects, as long as they are declared serializable.

// ON_SAVE_INSTANCE_STATE
// save instance data (5) on soft kill such as user changing phone orientation
protected void onSaveInstanceState(Bundle outState){
    password= editTextPassword.getText().toString(); 
    try {
        ConfuseTextStateBuilder b= ConfuseTextState.getBuilder();
        b.setIsShowCharCount(isShowCharCount);
        b.setTimeExpire(timeExpire); 
        b.setTimeoutType(timeoutType);
        b.setIsValidKey(isValidKey); 
        b.setPassword(password);
        state= b.build(); // may throw
    }
    catch(InvalidParameterException e){
        Log.d(TAG,"FailedToSaveState",e); // will be stripped out of runtime
    }
    outState.putSerializable("jalcomputing.confusetext.ConfuseTextState", state);  // save non view state
    super.onSaveInstanceState(outState); // save view state
    //Log.d(TAG,"onSaveInstance");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文