在编写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@CallSuperprotected 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后的回调
@CallSuperprotected void onStart() {mCalled = true;mFragments.doLoaderStart();getApplication().dispatchActivityStarted(this);}
onResume方法里,涉及与Application中activity resume后的回调
@CallSuperprotected void onResume() {getApplication().dispatchActivityResumed(this);mCalled = true;}
onPause方法里,涉及与Application中activity pause后的回调
@CallSuperprotected void onPause() {getApplication().dispatchActivityPaused(this);mCalled = true;}
onStop方法里,涉及与Application中activity stop后的回调
@CallSuperprotected void onStop() {if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);getApplication().dispatchActivityStopped(this);mCalled = true;}
onDestroy方法里,涉及到资源的释放,涉及与Application中activity destroy后的回调
@CallSuperprotected 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 dialogif (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变为trueif (!mCalled) {throw new SuperNotCalledException("Activity " + mComponent.toShortString() + " did not call through to super.onStart()");}//...}
可以看出,mCalled机制是一种强制措施,父类Activity中生命周期方法做了一些公共性的操作,这些操作对子类Activity来说是必不可少的。
这种代码设计,针对上述场景的需要,是一种不错的选择