简体   繁体   English

恢复活动状态

[英]Restoring the state of the activity

For restoring the state of the activity after it is recreated (for instance after the screen-orientation change) I implemented onSaveInstanceState() and onRestoreInstanceState(). 为了在重新创建活动之后(例如在更改屏幕方向之后)恢复活动的状态,我实现了onSaveInstanceState()和onRestoreInstanceState()。 It is simple to save/restore simple information like int, double etc. But what about saving/restoring objects like Timer? 保存/恢复诸如int,double等简单信息很简单。但是,如何保存/恢复诸如Timer之类的对象呢?

You cannot store "live" objects (like db connections) in the Activity arguments or saved instance data. 您不能在“活动”参数或已保存的实例数据中存储“活动”对象(例如db连接)。 Those mechanisms are designed so that the application can be completely stopped, so it only works with things that can be "serialized" and later restored. 设计这些机制是为了可以完全停止应用程序,因此它仅适用于可以“序列化”并在以后还原的事物。

What you can do is use fragments. 您可以做的就是使用片段。 If you add a fragment without UI (check here , look for “Adding a fragment without a UI”) and call on it setRetainInstance(true) the fragment will get reattached to the activity, surviving any configuration change. 如果添加不带UI的片段(请在此处查找“添加不带UI的片段”)并对其调用setRetainInstance(true),则该片段将重新附加到活动中,并保留所有配置更改。

Hope it helps. 希望能帮助到你。 (Remember you can use fragments with old Android versions by using the support package) (请记住,可以使用支持包将片段与旧的Android版本一起使用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM