zoukankan      html  css  js  c++  java
  • Can not perform this action after onSaveInstanceState

    IllegalStateException: Can not perform this action after onSaveInstanceState

    可能的原因之一:

    Fragment 在显示或者隐藏,移除是出现Can not perform this action after onSaveInstanceState  #解决办法:onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后 再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()

    可能的原因之二:Activity被系统回收之后,重建时恢复缓存的Fragment的原因  #解决办法之一:在Activity 回收时 onSaveInstanceState 中不缓存Fragment ,在OnCreate 中移除缓存相应Fragment数据,代码如下

    private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments";
           
            @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                if (savedInstanceState != null && this.clearFragmentsTag()) {
                    //重建时清除 fragment的状态
                    savedInstanceState.remove(BUNDLE_FRAGMENTS_KEY);
                }
                super.onCreate(savedInstanceState);
            }
         
         
            @Override
            protected void onSaveInstanceState(Bundle outState) {
                super.onSaveInstanceState(outState);
                if (outState != null && this.clearFragmentsTag()) {
                    //销毁时不保存fragment的状态
                    outState.remove(BUNDLE_FRAGMENTS_KEY);
                }
            }
         
            protected boolean clearFragmentsTag() {
                return true;
            }
  • 相关阅读:
    mybatis(十)缓存
    mybatis(八)复杂查询
    mybatis(六)分页
    mybatis(九)动态SQL
    mybatis(七)只用注解开发
    mybatis(五) 日志
    log4j.properties 相关配置
    mybatis(四)中可能出现的问题
    MyBatis(三) 配置解析
    IIS 发布 .net core 3.1
  • 原文地址:https://www.cnblogs.com/huhe/p/12313518.html
Copyright © 2011-2022 走看看