xxxActivity did not call through to super.onStart()等方法SuperNotCalledException的源码分析

在编写activity的过程中,会遇到类似以下异常信息:

throw new SuperNotCalledException(
                "Activity " + mComponent.toShortString() +
                " did not call through to super.onStart()");
一眼看去,知道是关于super.onCreate,super.onStart,super.onPause等方法的未被调用问题

往往是在生命周期方法中,添加调用super类对应的方法即可。那么,好奇的是,为什么一定要调用父类的方法呢?

从activity的方法说起,

onCreate方法里,涉及到ActionBar的设置,Bundle数据的操作,与Fragment的交互,Application中activity create后的回调

    @MainThread
    @CallSuper
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //...
        if (mActivityInfo.parentActivityName != null) {
            if (mActionBar == null) {
                mEnableDefaultActionBarUp = true;
            } else {
                mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
            }
        }
        if (savedInstanceState != null) {
            Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
            mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
                    ? mLastNonConfigurationInstances.fragments : null);
        }
        mFragments.dispatchCreate();
        getApplication().dispatchActivityCreated(this, savedInstanceState);
        if (mVoiceInteractor != null) {
            mVoiceInteractor.attachActivity(this);
        }
        mCalled = true;
    }

onStart方法里,涉及与Fragment的交互,Application中activity start后的回调

    @CallSuper
    protected void onStart() {
        mCalled = true;
        mFragments.doLoaderStart();
        getApplication().dispatchActivityStarted(this);
    }

onResume方法里,涉及与Application中activity resume后的回调

    @CallSuper
    protected void onResume() {
        getApplication().dispatchActivityResumed(this);
        mCalled = true;
    }
onPause方法里,涉及与Application中activity pause后的回调
    @CallSuper
    protected void onPause() {
        getApplication().dispatchActivityPaused(this);
        mCalled = true;
    }
onStop方法里,涉及与Application中activity stop后的回调
    @CallSuper
    protected void onStop() {
        if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
        getApplication().dispatchActivityStopped(this);
        mCalled = true;
    }
onDestroy方法里,涉及到资源的释放,涉及与Application中activity destroy后的回调
    @CallSuper
    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();
        }
        getApplication().dispatchActivityDestroyed(this);
    }

由此看出,对于父类的生命周期方法调用是必要的,不可少的

另外,在Activity中,有调用生命周期方法的方法,比如:

    final void performCreate(Bundle icicle) {
        onCreate(icicle);
        mActivityTransitionState.readState(icicle);
        performCreateCommon();
    }
还有performStart(),performResume(),performPause(),performStop(),performDestroy()等,称之为performXXX(),

performXXX()方法直接或者间接的调用对应的Activity生命周期方法,

而performXXX()方法是由系统去调用

在performXXX()内,会用一个成员变量mCalled,调用Activity生命周期方法之前,mCalled是false,mCalled最终会在Activity的生命周期方法内设置为true,如果为false,校验不通过,抛出SuperNotCalledException异常。也就是说,如果开发时自定义activity重写生命周期方法时,如果没有调用Super的对应方法,异常就会出现,如下:

    final void performStart() {
       //...
        mCalled = false;
        mInstrumentation.callActivityOnStart(this);//内部调用onStart()方法,mCalled变为true
        if (!mCalled) {
            throw new SuperNotCalledException("Activity " + mComponent.toShortString() + " did not call through to super.onStart()");
        }
        //...
    }
可以看出,mCalled机制是一种强制措施,父类Activity中生命周期方法做了一些公共性的操作,这些操作对子类Activity来说是必不可少的。

这种代码设计,针对上述场景的需要,是一种不错的选择


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值