9. super.onDestroy(); 應(yīng)該放在前面還是后面?為什么

本文目錄

  • 問題描述
  • 調(diào)研結(jié)果
  • 官方示例
  • 源碼解析

一. 問題描述:

**樣式 1 **:

@Override
    public void onDestroy() {
        // TODO:  some code
        super.onDestroy();
    }

**樣式 2 **:

@Override
    public void onDestroy() {
        super.onDestroy();
        // TODO:  some code
    }

相信很多人都會(huì)有困擾,哪一種方法更好呢?為什么,

  • 一方面很多人會(huì)支持把super放在第一行;
  • 另一方面很多人有怕界面銷毀后再執(zhí)行可能會(huì)拋出空指針
    那么哪一個(gè)是正確的順序呢?

二. 先拋出調(diào)研結(jié)果:

@Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        doCreate(savedInstanceState);
    }
    @Override
    protected final void onDestroy() {
        doDestroy();
        super.onDestroy();
    }
    @Override
    protected final void onResume() {
        super.onResume();
        doResume();
    }
    @Override
    protected final void onPause() {
        doPause();
        super.onPause();
    }
    @Override
    protected final void onStop() {
        doStop();
        super.onStop();
    }
    @Override
    protected final void onStart() {
        super.onStart();
        doStart();
    }
    @Override
    protected final void onSaveInstanceState(Bundle outState) {
        doSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

原因很多人一定很清楚,只是無法驗(yàn)證而已

三、官方示例

在Android源碼中大多數(shù)的源碼傾向于樣式一:
android.app.ListFragmentandroid.app.ListActivity 、android.speech.RecognitionService 等(當(dāng)前 Android API 25)

   //android.app.ListFragment:
    /**
     * @see Activity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        mHandler.removeCallbacks(mRequestFocus);
        super.onDestroy();
    }

四、解釋

  • super方法必須放在第一行嗎?
    由官方示例可以看出不是這樣的。
    很多人會(huì)支持把super放在第一行,說明基礎(chǔ)還是不夠扎實(shí)
    在Java語法中確實(shí)是在子類構(gòu)造函數(shù)中super()必須放在第一行,注意是構(gòu)造函數(shù);以此來優(yōu)先構(gòu)造父類對(duì)象.
    而父類方法卻沒有這樣的要求。

  • 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);
    }

繼續(xù)往下走: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);
            }
        }
    }

繼續(xù)往下走:

//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()后再寫操作可能會(huì)導(dǎo)致操作對(duì)象爆出NullPointerException;(只有一行代碼之隔 概率還是很低的,但理論上是存在的)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容