Activity的performDestroy用于执行ActivityDestroy,代码如下:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
final void performDestroy() {
if (Trace.isTagEnabled(Trace.TRACE_TAG_WINDOW_MANAGER)) {
Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "performDestroy:"
+ mComponent.getClassName());
}
dispatchActivityPreDestroyed();
mDestroyed = true;
mWindow.destroy();
mFragments.dispatchDestroy();
onDestroy();
EventLogTags.writeWmOnDestroyCalled(mIdent, getComponentName().getClassName(),
"performDestroy");
mFragments.doLoaderDestroy();
if (mVoiceInteractor != null) {
mVoiceInteractor.detachActivity();
}
dispatchActivityPostDestroyed();
Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
}
}
上面方法主要处理如下:
1、调用Activity的dispatchActivityPreDestroyed方法。
2、调用Activity的onDestroy方法。
3、调用Activity的dispatchActivityPostDestroyed方法。
下面分别进行分析:
Activity dispatchActivityPreDestroyed
调用Activity的dispatchActivityPreDestroyed方法:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
private void dispatchActivityPreDestroyed() {
getApplication().dispatchActivityPreDestroyed(this);
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = callbacks.length - 1; i >= 0; i--) {
((Application.ActivityLifecycleCallbacks) callbacks[i])
.onActivityPreDestroyed(this);
}
}
}
}
Application.ActivityLifecycleCallbacks onActivityPreDestroyed
调用Application.ActivityLifecycleCallbacks的onActivityPreDestroyed方法,如果应用程序调用了Application的registerActivityLifecycleCallbacks,就会收到onActivityPreDestroyed回调。
void onActivityPreDestroyed(@NonNull Activity activity) :作为被销毁的活动的第一步调用。这总是在 {@link Activity#onDestroy} 之前调用。
Activity onDestroy
调用Activity的onDestroy方法,在Activity的子类(应用程序实现)的onDestroy会调用super.onDestroy()方法:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
protected void onDestroy() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
mCalled = true;
getAutofillClientController().onActivityDestroyed();
// 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();
}
if (mActionBar != null) {
mActionBar.onDestroy();
}
dispatchActivityDestroyed();
notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_STOP);
if (mUiTranslationController != null) {
mUiTranslationController.onActivityDestroyed();
}
if (mDefaultBackCallback != null) {
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mDefaultBackCallback);
mDefaultBackCallback = null;
}
if (mCallbacksController != null) {
mCallbacksController.clearCallbacks();
}
}
}
Activity dispatchActivityDestroyed
调用Activity的dispatchActivityDestroyed方法:
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
private void dispatchActivityDestroyed() {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = callbacks.length - 1; i >= 0; i--) {
((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(this);
}
}
getApplication().dispatchActivityDestroyed(this);
}
}
Application.ActivityLifecycleCallbacks onActivityDestroyed
调用Application.ActivityLifecycleCallbacks的onActivityDestroyed方法,如果应用程序调用了Application的registerActivityLifecycleCallbacks,就会收到onActivityDestroyed回调。
void onActivityDestroyed(@NonNull Activity activity):当 Activity 调用 {@link Activity#onDestroy super.onDestroy()} 时调用。
Activity dispatchActivityPostDestroyed
调用Activity的dispatchActivityPostDestroyed方法:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
private void dispatchActivityPostDestroyed() {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = callbacks.length - 1; i >= 0; i--) {
((Application.ActivityLifecycleCallbacks) callbacks[i])
.onActivityPostDestroyed(this);
}
}
getApplication().dispatchActivityPostDestroyed(this);
}
}
Application.ActivityLifecycleCallbacks onActivityPostDestroyed
调用Application.ActivityLifecycleCallbacks的onActivityPostDestroyed方法,如果应用程序调用了Application的registerActivityLifecycleCallbacks,就会收到onActivityPostDestroyed回调。
void onActivityPostDestroyed(@NonNull Activity activity) :作为被销毁的活动的第一步调用。这总是在 {@link Activity#onDestroy} 之前调用。