如果 onDestroy 之后有一个 Instruction,这个 Instruction 会被执行吗?

If there is a Instruction after onDestroy , will the Instruction be executed?

在 Destroy 之后我打印一条 Log 语句,Log 是否会显示为应用程序已经被销毁并且在应用程序被销毁后无法执行任何指令。

    @Override
protected void onDestroy() {
    super.onDestroy();
    Log.v(TestActivity,"App is currntly getting destroyed")
}

那么"App is currently getting destroyed"会被打印出来吗? 如果它不会被打印,那么我如何在 onDestroy 方法中执行代码?

在您的代码执行后调用 super.onDestory()

@Override
protected void onDestroy() {
    Log.v(MainActivity,"App is currntly getting destroyed")
    super.onDestroy();
}

如果应用程序被 android 销毁以分配内存,也不会调用它。 最好使用 onStop() 方法来保存数据。

不保证onDestroy() 会一直调用。你可以在 onStop() 方法中做你的清洁工作。

你的问题的答案是,"It Depends"。

reference documentationonDestroy 不能保证被调用。

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. 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.

所以,是的,您的消息将被调用 ,除非 Android 先销毁进程

文档还说你 必须 调用超级 classes 实现...

If you override this method you must call through to the superclass implementation.

...但它没有说明调用的顺序。因此,我会查看 Activiy 源代码以了解它在做什么。这是来自 Google Source.

onDestroy 的(最新版本)源代码
protected void onDestroy() {
    mCalled = true;
    // dismiss any dialogs we are managing.
    if (mManagedDialogs != null) {
        final int numDialogs = mManagedDialogs.size();
        for (int i = 0; i < numDialogs; i++) {
            final ManagedDialog md = mManagedDialogs.valueAt(i);
            if (md.mDialog.isShowing()) {
                md.mDialog.dismiss();
            }
        }
        mManagedDialogs = null;
    }
    // close any cursors we are managing.
    synchronized (mManagedCursors) {
        int numCursors = mManagedCursors.size();
        for (int i = 0; i < numCursors; i++) {
            ManagedCursor c = mManagedCursors.get(i);
            if (c != null) {
                c.mCursor.close();
            }
        }
        mManagedCursors.clear();
    }
    // Close any open search dialog
    if (mSearchManager != null) {
        mSearchManager.stopSearch();
    }
}

尽管这可能随时更改,但我们可以看到超级 class 中的 onDestroy 将清理资源(如文档所述)。那么,您对 onDestroy 的实施是否依赖于任何这些资源?这将决定您的代码应该在 super.onDestroy() 之前被调用。如果不是,那么顺序无关紧要(除了你的老师已经告诉你使用什么顺序)。