放在第一行:
@Override
public void onDestroy() {
// TODO: some code
super.onDestroy();
}
放在最后一行:
@Override
public void onDestroy() {
super.onDestroy();
// TODO: some code
}
onDestroy到底做了那些事情?
看源码:
protected void onDestroy() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
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();
}
if (mActionBar != null) {
mActionBar.onDestroy();
}
getApplication().dispatchActivityDestroyed(this);
}
继续往下走:
android.app.Application#dispatchActivityDestroyed
/* package */
void dispatchActivityDestroyed(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity);
}
}
}
//activity 将从栈中移除
public void onActivityDestroyed(Activity activity) {
LogUtil.d("GIO.AppState", new Object[]{"onActivityDestroyed ", activity});
this.a().remove(activity);
this.t.remove(activity);
this.u.remove(activity);
}
所以在super()后再写操作可能会导致操作对象爆出NullPointerException;(只有一行代码之隔 概率还是很低的,但理论上是存在的)