Activity之生命周期

  1. 正常情况下的生命周期
    A->B
    AonPause-BonCreate-BonStart-BonResume(-AonStop这个是否执行取决于A是否被全部覆盖)
    B->A
    BonPause-AonStart-AonResume-BonStop-BonDestroy
    当Activity切换时,需要先将栈顶Activity置为pause,再执行新Activity的方法(此时栈顶为新的Activity)。
    所以如果有保存数据耗时操作的话,尽量在onStop中执行,是为了不妨碍新Activity的页面展现。而设置或者取数据等操作可以放在onResume中执行。
  2. 横竖屏切换时的生命周期
    2.1 不设置configChange
    切至横屏:
onPause
            onSaveInstanceState[Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@d414664, 2131427397=android.view.AbsSavedState$1@d414664, 2131427398=android.view.AbsSavedState$1@d414664, 2131427399=android.support.v7.widget.Toolbar$SavedState@9efe2af, 2131427400=android.view.AbsSavedState$1@d414664, 2131427417=android.view.AbsSavedState$1@d414664, 2131427418=TextView.SavedState{48f06bc start=0 end=0 text=}}, android:focusedViewId=2131427418}]}]]
            onStop
            onDestroy
            onCreate:saveInstanceState[Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@d414664, 2131427397=android.view.AbsSavedState$1@d414664, 2131427398=android.view.AbsSavedState$1@d414664, 2131427399=android.support.v7.widget.Toolbar$SavedState@9efe2af, 2131427400=android.view.AbsSavedState$1@d414664, 2131427417=android.view.AbsSavedState$1@d414664, 2131427418=TextView.SavedState{48f06bc start=0 end=0 text=}}, android:focusedViewId=2131427418}]}]]
            onStart
            onRestoreInstanceState[Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@d414664, 2131427397=android.view.AbsSavedState$1@d414664, 2131427398=android.view.AbsSavedState$1@d414664, 2131427399=android.support.v7.widget.Toolbar$SavedState@9efe2af, 2131427400=android.view.AbsSavedState$1@d414664, 2131427417=android.view.AbsSavedState$1@d414664, 2131427418=TextView.SavedState{48f06bc start=0 end=0 text=}}, android:focusedViewId=2131427418}]}]]
            onResume

切至竖屏:

onPause
            onSaveInstanceState[Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@d414664, 2131427397=android.view.AbsSavedState$1@d414664, 2131427398=android.view.AbsSavedState$1@d414664, 2131427399=android.support.v7.widget.Toolbar$SavedState@e6fed31, 2131427400=android.view.AbsSavedState$1@d414664, 2131427417=android.view.AbsSavedState$1@d414664, 2131427418=TextView.SavedState{4d31c16 start=0 end=0 text=}}, android:focusedViewId=2131427418}]}]]
            onStop
            onDestroy
            onCreate:saveInstanceState[Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@d414664, 2131427397=android.view.AbsSavedState$1@d414664, 2131427398=android.view.AbsSavedState$1@d414664, 2131427399=android.support.v7.widget.Toolbar$SavedState@e6fed31, 2131427400=android.view.AbsSavedState$1@d414664, 2131427417=android.view.AbsSavedState$1@d414664, 2131427418=TextView.SavedState{4d31c16 start=0 end=0 text=}}, android:focusedViewId=2131427418}]}]]
            onStart
            onRestoreInstanceState[Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@d414664, 2131427397=android.view.AbsSavedState$1@d414664, 2131427398=android.view.AbsSavedState$1@d414664, 2131427399=android.support.v7.widget.Toolbar$SavedState@e6fed31, 2131427400=android.view.AbsSavedState$1@d414664, 2131427417=android.view.AbsSavedState$1@d414664, 2131427418=TextView.SavedState{4d31c16 start=0 end=0 text=}}, android:focusedViewId=2131427418}]}]]
            onResume

保存数据:

Activity.java:
            final void performSaveInstanceState(Bundle outState) {
                onSaveInstanceState(outState);  // 保存页面信息
                saveManagedDialogs(outState);  // 保存Dialog信息
                mActivityTransitionState.saveState(outState);  // 保存页面跳转信息
                storeHasCurrentPermissionRequest(outState);
            }

            protected void onSaveInstanceState(Bundle outState) {
                outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());  // 保存Window信息
                Parcelable p = mFragments.saveAllState();
                if (p != null) {
                    outState.putParcelable(FRAGMENTS_TAG, p);  // 保存Fragment信息
                }
                getApplication().dispatchActivitySaveInstanceState(this, outState);
            }

        PhoneWindow.java:
            public Bundle saveHierarchyState() {
                Bundle outState = new Bundle();
                if (mContentParent == null) {
                    return outState;
                }

                SparseArray<Parcelable> states = new SparseArray<Parcelable>();
                mContentParent.saveHierarchyState(states);  // 保存页面View信息
                outState.putSparseParcelableArray(VIEWS_TAG, states);

                // Save the focused view ID.
                final View focusedView = mContentParent.findFocus();
                if (focusedView != null && focusedView.getId() != View.NO_ID) {
                    outState.putInt(FOCUSED_ID_TAG, focusedView.getId());
                }

                // save the panels
                SparseArray<Parcelable> panelStates = new SparseArray<Parcelable>();
                savePanelState(panelStates);
                if (panelStates.size() > 0) {
                    outState.putSparseParcelableArray(PANELS_TAG, panelStates);
                }

                if (mDecorContentParent != null) {
                    SparseArray<Parcelable> actionBarStates = new SparseArray<Parcelable>();
                    mDecorContentParent.saveToolbarHierarchyState(actionBarStates);
                    outState.putSparseParcelableArray(ACTION_BAR_TAG, actionBarStates);
                }

                return outState;
            }

        View.java:
            public void saveHierarchyState(SparseArray<Parcelable> container) {
                dispatchSaveInstanceState(container);
            }

            protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
                if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {  // 设置了id且没有disableSave
                    mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
                    Parcelable state = onSaveInstanceState();
                    if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
                        throw new IllegalStateException(
                                "Derived class did not call super.onSaveInstanceState()");
                    }
                    if (state != null) {
                        // Log.i("View", "Freezing #" + Integer.toHexString(mID)
                        // + ": " + state);
                        container.put(mID, state);
                    }
                }
            }

            protected Parcelable onSaveInstanceState() {
                mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
                if (mStartActivityRequestWho != null) {
                    BaseSavedState state = new BaseSavedState(AbsSavedState.EMPTY_STATE);
                    state.mStartActivityRequestWhoSaved = mStartActivityRequestWho; // 保存mStartActivityRequestWho
                    return state;
                }
                return BaseSavedState.EMPTY_STATE;
            }
        ViewGroup.java:
            protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
                super.dispatchSaveInstanceState(container);
                final int count = mChildrenCount;
                final View[] children = mChildren;
                for (int i = 0; i < count; i++) {
                    View c = children[i];
                    if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
                        c.dispatchSaveInstanceState(container);  // 下发到每一个子View自己的dispatchSaveInstanceState处理
                    }
                }
            }

2.2 设置configChanges=”orientation|screenSize”
切至横屏:onConfigurationChanged[{1.0 ?mcc?mnc zh_CN ldltr sw360dp w598dp h336dp 480dpi nrml land finger -keyb/v/h -nav/h suim:1 s.16}]
切至竖屏:onConfigurationChanged[{1.0 ?mcc?mnc zh_CN ldltr sw360dp w360dp h580dp 480dpi nrml port finger -keyb/v/h -nav/h suim:1 s.17}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值