Can not perform this action after onSaveInstanceState

java.lang.IllegalStateException

Can not perform this action after onSaveInstanceState

1 android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:4)
2 android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1)
3 android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:12)
4 android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:1)
5 android.support.v4.app.DialogFragment.show(DialogFragment.java:5)

一般出现场景都是开启一个异步任务 结束之后使用DialogFragment进行展示内容 ,接下来给大家分析下为何会发生这样异常,如何避免

public void show(FragmentManager manager, String tag) {
    this.mDismissed = false;
    this.mShownByMe = true;
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commit();
}
public int commit() {
    return this.commitInternal(false);
}

在看commistInternal方法

int commitInternal(boolean allowStateLoss) {
    if (this.mCommitted) {
        throw new IllegalStateException("commit already called");
    } else {
        if (FragmentManagerImpl.DEBUG) {
            Log.v("FragmentManager", "Commit: " + this);
            LogWriter logw = new LogWriter("FragmentManager");
            PrintWriter pw = new PrintWriter(logw);
            this.dump("  ", (FileDescriptor)null, pw, (String[])null);
            pw.close();
        }

        this.mCommitted = true;
        if (this.mAddToBackStack) {
            this.mIndex = this.mManager.allocBackStackIndex(this);
        } else {
            this.mIndex = -1;
        }

        this.mManager.enqueueAction(this, allowStateLoss);
        return this.mIndex;
    }
}

这里参数传的是false 标示显示的fragment不允许状态丢失,接下来看enqueueAction方法

public void enqueueAction(FragmentManagerImpl.OpGenerator action, boolean allowStateLoss) {
    if (!allowStateLoss) {
        this.checkStateLoss();
    }

    synchronized(this) {
        if (!this.mDestroyed && this.mHost != null) {
            if (this.mPendingActions == null) {
                this.mPendingActions = new ArrayList();
            }

            this.mPendingActions.add(action);
            this.scheduleCommit();
        } else if (!allowStateLoss) {
            throw new IllegalStateException("Activity has been destroyed");
        }
    }
}

我们可以看到如果不允许状态丢失会check一下 在看checkStateLoss方法

private void checkStateLoss() {
    if (this.isStateSaved()) {
        throw new IllegalStateException("Can not perform this action after onSaveInstanceState");
    } else if (this.mNoTransactionsBecause != null) {
        throw new IllegalStateException("Can not perform this action inside of " + this.mNoTransactionsBecause);
    }
}

首先判断了状态是否已经报错 true则抛出异常,在看如何进行判断的isStateSaved方法

public boolean isStateSaved() {
    return this.mStateSaved || this.mStopped;
}

有两个表示值mStateSaved 和mStopped 只要有任何一个状态满足则返回true

由此可以总结出当activity调用了 onSaveInstanceState方法后或者activity处于stopped状态时候 使用FragmentManager的FragmentTransaction.commit方法提交抛出这样"Can not perform this action after onSaveInstanceState"异常 直接导致程序crash

解决方案也就很简单了 只要保证activity的状态 不是isStateSaved状态使用FragmentManager的FragmentTransaction.commit 提交fragment就可以了

ps多看看源码有助于我们更好的使用系统提供的api

文章有什么不对的地方大家斧正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值