即使 phone 因电量不足而死亡,是否会调用 onPause() 或 onStop()?
Is onPause() or onStop() called even if phone dies due to lack of power?
例如,如果我需要保留一些用户可以在我的应用程序中编辑的非常重要的数据,我是否需要在每次用户更改此类数据时保存它们,或者我是否可以将其保存在 onPause 中()、onStop() 或 onDestroy() 方法?
可以以某种方式 应用程序在不调用任何这些方法的情况下结束吗? (例如当电池耗尽时)
在这种情况下,请通过调试界面验证您的 phone activity,某些 phone 会终止您的应用程序,这是强制退出。
这在onDestroy()中肯定做不到。
根据 documentation:
There are situations where the system will simply kill the activity's
hosting process without calling this method (or any others) in it, so
it should not be used to do things that are intended to remain around
after the process goes away.
是的,应用程序可以在不调用任何生命周期方法的情况下结束。
在 phone 关闭的情况下,您可以使用 ACTION_SHUTDOWN Intent。
更多信息 here。对于所有其他情况,onPause 应该可以完成工作。即使在提供的 link 中,也有声明如果应用程序处于 FG 将调用 onPause。
Apps will not normally need to handle this, since the foreground
activity will be paused as well.
但是,如果不是那么昂贵的操作,我会在编辑后保存数据。
根据 Android activity 生命周期的 documentation,onPause
在 activity 进入背景,但尚未(尚未)被杀死。
因此,在大多数 Android 文档和示例代码中,您会发现 onPause
用于保存 activity 正在编辑的任何持久状态,以呈现 "edit in place" 向用户建模并确保在没有足够资源的情况下不会丢失任何内容。
所以在你的用例中,你需要做的就是为你的 Activity
实现 onPause
并编写代码来保存 activity 状态(EditText
内容或任何其他正在进行的用户交互)。 Android 系统将保存 activity 状态,当 android 下次启动您的 activity 时,您可以随时返回 Activity 的 onCreate
状态。
例如,如果我需要保留一些用户可以在我的应用程序中编辑的非常重要的数据,我是否需要在每次用户更改此类数据时保存它们,或者我是否可以将其保存在 onPause 中()、onStop() 或 onDestroy() 方法?
可以以某种方式 应用程序在不调用任何这些方法的情况下结束吗? (例如当电池耗尽时)
在这种情况下,请通过调试界面验证您的 phone activity,某些 phone 会终止您的应用程序,这是强制退出。
这在onDestroy()中肯定做不到。 根据 documentation:
There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
是的,应用程序可以在不调用任何生命周期方法的情况下结束。
在 phone 关闭的情况下,您可以使用 ACTION_SHUTDOWN Intent。 更多信息 here。对于所有其他情况,onPause 应该可以完成工作。即使在提供的 link 中,也有声明如果应用程序处于 FG 将调用 onPause。
Apps will not normally need to handle this, since the foreground activity will be paused as well.
但是,如果不是那么昂贵的操作,我会在编辑后保存数据。
根据 Android activity 生命周期的 documentation,onPause
在 activity 进入背景,但尚未(尚未)被杀死。
因此,在大多数 Android 文档和示例代码中,您会发现 onPause
用于保存 activity 正在编辑的任何持久状态,以呈现 "edit in place" 向用户建模并确保在没有足够资源的情况下不会丢失任何内容。
所以在你的用例中,你需要做的就是为你的 Activity
实现 onPause
并编写代码来保存 activity 状态(EditText
内容或任何其他正在进行的用户交互)。 Android 系统将保存 activity 状态,当 android 下次启动您的 activity 时,您可以随时返回 Activity 的 onCreate
状态。