如果 Activity 被 OS 杀死,我们如何检索保存的状态?

How do we retrieve saved state if the Activity is killed by OS?

所以,据我了解,一旦App没有被销毁并且处于后台;如果 OS 需要更多内存,则 OS 会终止应用程序但会保存状态 (onSaveInstanceState)。当我们重新打开应用程序时,看起来我们面对的是我们之前的 activity 但实际上它已经被销毁并重新创建。如果我的解释是正确的,应用程序如何检索保存的状态?它存储在内存中吗?我们能够检索保存的状态多长时间?

OS 存储它。它调用 onSaveInstanceState 让你生成一个它存储的 Bundle,并且会调用 onRestoreInstanceState 让你从状态中恢复自己。 OS 如何存储它并不重要——也许它将它保存在 RAM 中,也许它将它序列化到磁盘。可以肯定的是,如果您返回到 activity,您将收到一个 Bundle 对象,其中包含您之前填写的信息。您不需要检索状态 - 如果它存在,它将被传递给您。

If my interpretation is correct, how does the App retrieve the saved state?

来自Android documentation

If the system destroys the activity due to system constraints (such as a configuration change or memory pressure), then although the actual Activity instance is gone, the system remembers that it existed. If the user attempts to navigate back to the activity, the system creates a new instance of that activity using a set of saved data that describes the state of the activity when it was destroyed.

关于您的第二个问题 - 这是 OS 如何实现的实现细节,实际上我们不应该担心 :)。重要的是它应该可靠地做到这一点。

只要用户不按Back或者你的Activityfinish()没有被调用,系统就会保持保存状态。