仅当您显式调用 finish() 时才会调用 onDestroy 吗?或者有什么例外吗?

Is onDestroy called only if you explicitly call finish() ?? or are there any exceptions?

我有一个 LocalBroadcastReceiver,我 unregistering 它在我的 ondestroy() 中。

现在我读到 ondestroy() 在这两个 SO 答案 is-ondestroy-not-always-called and why-implement-ondestroy-if-it-is-not-guaranteed-to-be-called and as well as in Androi Docs 中提到

onDestroy will be called if you explicitly call finish();

但为什么在我的情况下我没有调用 finish() 但仍然在我的所有 Android 设备中每次都调用 ondestroy()。另外根据你们的说法,在哪些情况下 ondestroy() 没有被召集。

此外,即使 Android 会杀死我的应用程序(由于内存较少),我也不必担心,因为 Android 会杀死我的整个应用程序,因此接收器最终会被杀死。 (所以不会有任何内存泄漏)。

所以对于我的用例,哪些情况 ondestroy() 不会被调用。

But why in my case I am not calling finish() but still ondestroy() is getting called everytime in all of the Android devices.

onBackPressed() 的默认实现 — 返回按钮触发的内容 — 调用 finish()

where are the cases where ondestroy() not get called up.

  1. 如果您因未处理的异常而崩溃

  2. 如果您的进程以紧急方式终止(例如,系统需要 RAM 来处理传入的 phone 调用)

  3. 如果用户在“设置”中点击您应用屏幕上的"Force Stop"

  4. 在一些设备上,如果用户使用制造商提供的任务管理器终止您的进程

Also even if Android will kill my app(due to less memory) I don't need to worry as Android is going to kill my whole app so receiver will ultimately get killed

是的,因为你的整个进程都会被终止。