简体   繁体   English

Android上的活动娱乐

[英]Activity Recreation on Android

I am reading the official Android tutorial on Managing Activity Lifecycle. 我正在阅读有关管理活动生命周期的官方Android教程。 Please have a look at the following points as I quote. 请引用以下几点。

(Normal app behavior) "When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed." (正常的应用程序行为)“当由于用户按下Back或活动完成而破坏了活动时,该活动实例的系统概念将永远消失,因为该行为表明不再需要该活动。”

(System induced destruction) "If the system destroys the activity due to system constraints (rather than normal app behavior), then althought the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed." (系统诱发的破坏)“如果系统由于系统约束(而不是正常的应用行为)破坏了活动,则尽管实际的Activity实例已消失,但系统会记住它的存在,以便用户导航回该实例时,系统使用一组保存的数据创建活动的新实例,这些数据描述了活动被销毁时的状态。”

"The saved data that the system uses to restore the previous state is a collection of key value pairs stored in a Bundle object. By default the saved information is about the activity's view hierarchy, such as the text entered in a widget, the scroll position, etc. To save additional state information for your activity, such as member variables, you must implement onSaveInstanceState() and add key-value pairs to the Bundle object." “系统用来恢复先前状态的已保存数据是存储在Bundle对象中的一组键值对。默认情况下,已保存的信息与活动的视图层次结构有关,例如在小部件中输入的文本,滚动位置等等。要保存活动的其他状态信息(例如成员变量),必须实现onSaveInstanceState()并将键值对添加到Bundle对象。”

Please see if I can sum up correctly from the above: 请查看我是否可以从以上内容正确总结:

The system is resource conscious and can destroy an activity to recover memory. 该系统具有资源意识,并且可以破坏活动以恢复内存。 If the system does that then it leaves an option of recovering the destroyed activity to its previous state if needed later. 如果系统执行了该操作,则可以保留将被销毁的活动恢复到先前状态的选项,如果以后需要的话。

State = view-state (default, done automatic) + member-variable-state (manual, up to the programmer). 状态=视图状态(默认,自动完成)+成员变量状态(手动,由程序员决定)。

My question is, if the system is destroying an activity in order to free some memory, then doesn't it defeat the purpose if it allows us to keep a "copy" of the activity so that the activity can be restored to the point exactly as it was prior to destruction? 我的问题是,如果系统为了释放一些内存而破坏了一个活动,那么,如果它允许我们保留该活动的“副本”,以便该活动可以准确地还原到一点,它是否会破坏目的?就像在销毁之前一样?

No, it does not defeat the purpose: 不,它不会破坏目的:

  • A Bundle is optimized for being serializable, so the Android framework could just write it to disk. Bundle针对可序列化进行了优化,因此Android框架可以将其写入磁盘。
  • The data you need to represent your state is much smaller than the amount of RAM you need for a running copy of your app. 表示状态所需的数据比运行应用程序副本所需的RAM量小得多。 For example, if you show an image to the user, you'd probably just have to save the image's location, not all of its data. 例如,如果向用户显示图像,则可能只需要保存图像的位置,而不是保存所有数据。 Also, every Android app is a whole new Dalvik VM, so if all the activities are from different applications, killing an activity means one Dalvik VM less – and every Dalvik VM probably needs a relatively big amount of RAM. 另外,每个Android应用程序都是一个全新的Dalvik VM,因此,如果所有活动都来自不同的应用程序,则杀死一个活动意味着要少一个Dalvik VM –每个Dalvik VM可能都需要相对大量的RAM。

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

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