super.onDestroy(); 应该放在前面还是后面?为什么?

一. 问题描述:

**样式 1 **:

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

**样式 2 **:

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

相信很多人都会有困扰,哪一种方法更好呢?为什么,

  • 一方面很多人会支持把super放在第一行;
  • 另一方面很多人有怕界面销毁后再执行可能会抛出空指针
    那么哪一个是正确的顺序呢?

二. 先抛出调研结果:

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

原因很多人一定很清楚,只是无法验证而已

三、官方示例

在Android源码中大多数的源码倾向于样式一:
android.app.ListFragmentandroid.app.ListActivityandroid.speech.RecognitionService 等(当前 Android API 25)

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

四、解释

  • super方法必须放在第一行吗?
    由官方示例可以看出不是这样的。
    很多人会支持把super放在第一行,说明基础还是不够扎实
    在Java语法中确实是在子类构造函数中super()必须放在第一行,注意是构造函数;以此来优先构造父类对象.
    而父类方法却没有这样的要求。

  • 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;(只有一行代码之隔 概率还是很低的,但理论上是存在的)



作者:android_赵乐玮
链接:https://www.jianshu.com/p/0aa29c9f9ab3
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

发布了30 篇原创文章 · 获赞 13 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/wjr1949/article/details/88745746