美文网首页Android技术知识Android开发
Can not perform this action afte

Can not perform this action afte

作者: 支离破碎_SuperLee | 来源:发表于2021-05-12 11:51 被阅读0次

    如何解决Can not perform this action after onSaveInstanceState

    先分析问题发生原因

    ```

    private void checkStateLoss() {

        if (isStateSaved()) {

            throw new IllegalStateException(

            "Can not perform this action after onSaveInstanceState");

        }

    }

    public boolean isStateSaved() {

        // See saveAllState() for the explanation of this.  We do this for

        // all platform versions, to keep our behavior more consistent between

        // them.

        return mStateSaved ||mStopped;

    }

    ```

    通过分析源码我们找到,当activity执行了onSaveInstanceState或onStop方法后,操作子fragment系统会检测当前状态并抛出异常。

    解决方法:

    配合Lifecycle,我们只需要重写添加:

    ```

    lifecycleScope?.launchWhenResumed{

        //show fragment dialog

    }

    ```

    这样即使是activity在后台情况下返回前台,也会立即弹窗。

    相关文章

      网友评论

        本文标题:Can not perform this action afte

        本文链接:https://www.haomeiwen.com/subject/rxirdctx.html