Android can not perform this action after onSaveInstanceState 问题解决

解决Can not perform this action after onSaveInstanceState异常总结 上篇博客从源码层面分析了Can not perform this action after onSaveInstanceState异常产生的原因及流程, 接下来分析下该崩溃的设计原因以及如何避。 安卓为什么要抛出该异常呢? 都知道系统在内存吃紧时会按规则优先kill掉部分非前台activity,为了保证用户体验,系统在kill掉某个activity之前会先调用onSaveInstanceSt 阅读详情

今天遇到了一个这样的问题,我写了一个MainActivity,里面有一个NavigationDrawer,其中的内容自然是使用的Fragment,当我在MainActivity上弹出一个Dialog样式的Activity并且这个Activity将要关闭的时候,程序会Crash掉,报出一个 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState的错误,通过十几个Log连续输出跟踪,发现时在修改Fragment的commit的时候出现了异常,查看开发者文档,是这样写的

public abstract int commit ()
Added in API level 11

Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.

Returns
  • Returns the identifier of this transaction's back stack entry, if addToBackStack(String) had been called. Otherwise, returns a negative number.

首先,他说commit不是立即执行,而是当主线程在下一次准备好的时候进行修改,也就是说,这个函数只是把请求放到队列中去。注意中间的部分,他说commit操作必须在父容器saving state之前,因为当父容器saving state,意味着父容器可能会被销毁,这个时候这个commit可能会变成无效的。

回过头来看我们的错误,应该就是这个问题,所以下面的的解决方式就是使用commitAllowingStateLoss()方法,我们再看一下这个方法的介绍

public abstract int commitAllowingStateLoss ()
Added in API level 11

Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

很明显这就是我们想要的方法,允许在MainActivity执行onSaveInstanceState后进行Fragment修改。

解决 IllegalStateException: Can not perform this action after onSaveInstanceState IllegalStateException: Can not perform this action after onSaveInstanceState异常出现的场景分析及解决方法 阅读详情

相关推荐

Can not perform this action after onSaveInstanceState异常源码分析

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 在使用fragment、dialogFragment等控件时可能会遇到以上异常。原因正如抛出的异常所说一样:不能在onSaveInstanceState之后执行该操作。

吧啦吧啦F 3782

Android Bugs——DialogFragment.showNow:Can not perform this action after onSaveInstanceState

那么DialogFragment的show方法是否也可以按照此思路去修改呢,答案是肯定的。在 Activity 被销毁之前被调用来保存每个实例的状态,这样就可以保证该状态能够从。activity的onSaveInstanceState方法中会调用。,所以报错原因就不言而喻了,那么下面说下解决方案。我们都知道Fragment的事务提交有两个方法。方法,来进行Fragment的状态保存,这两种情况都是在存储状态后调用了。,用来标识状态已被保存过。好,下面回到我们报错的地方。等方法,这些方法会调用。

小欣儿的专栏 1478

Android之提示错误Can not perform this action after onSaveInstanceState

1 问题 主页面3个Fragment,在第三个Fragment里面开启了Activity之后,然后想跳到第一个Fragment代码如下 /** * 展示Fragment */ private fun showFragment(fragment: Fragment) { if (currentFragment !== fragment) { val transaction: FragmentTransaction = supp

码莎拉蒂 4973

Android异常篇 IllegalStateException: Can not perform this action after onSaveInstanceState

一、官方文档解析 public abstract int commit () Added in API level 11 Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready. A transaction can on

圈池 2113

IllegalStateException: Can not perform this action after onSaveInstanceState

问题:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState                                                                               at android.support.v4.app.Fragme...

LXFX1108的专栏 564

Fragment实用细节技巧

fragment.setRetainInstance(true) from layout 和 add fragment区别

在路上-codingAndlearning 882

android:Can not perform this action after onSaveInstanceState解决方法

今天在做项目的时候遇到这个问题:android:Can not perform this action after onSaveInstanceState;看了下fragment的源码 /** * Schedules a commit of this transaction. The commit does * not happen immediately; it wil

churck_android 992

利用Python实现纬度加权平均(即NCL中的wgt_areaave)

当处理全球数据的时候,最常见的分析就是全球、北半球平均了,这时候一定要注意要根据纬度等信息进行加权,求加权平均,要不然结果肯定会不准。但是对于Python中,常规的计算包里面是没有的,或许现在有其他包可以实现,也可以告诉我~。

记录基于python语言处理气象问题的一些代码,包括不限于气象数据读取、机器学习和深度学习建模。 5739

【Android】【异常处理】解决FragmentTransaction Can not perform this action after onSaveInstanceState问题

???? 出现原因 一般是由于离开Activity进入其他界面,这时Activity被回收 再回到Activity时,Activity会重启恢复之前的状态 在Activity尚未完全恢复时,过早地执行了Fragment的commit方法,就会引发这个问题 ???? 原理 Activity在销毁前,会执行onSaveInstanceState保存自身状态 Activity在恢复时,会调用onRestoreIns...

天天向上 Up Up Up (*^▽^*) 891

增程式永磁电机发电系统电压环二自由度控制器设计

本论文主要研究增程式永磁电机发电系统的电压环二自由度控制器设计。该系统采用二自由度控制策略,通过精确调节电压环和电流环,显著提高系统的动态响应性能和稳态稳定性。基于SVPWM(空间矢量脉宽调制)技术,本研究设计的电压环控制器能够在负载突变和外部扰动的情况下,快速恢复并保持电压稳定,从而满足增程式永磁电机发电系统对电压精度和稳定性的高要求。仿真分析表明,所设计的控制器在不同工况下展现出良好的电压稳定性、较短的响应时间和较低的稳态误差,能够有效抑制电压波动并提高系统的鲁棒性。实验结果表明,控制器在维持高效能和高

weixin_44553616的博客 893

ur3手眼标定+realsenseL515

知乎 csdn

AI_future的博客 970

Fragment全解析系列(一,音视频开发进阶指南

在系统要把app回收之前,系统会把Activity的状态保存下来,Activity的FragmentManager负责把Activity中的Fragment保存起来。在“内存重启”后,Activity的恢复是从栈顶逐步恢复,Fragment会在宿主Activity的onCreate方法调用后紧接着恢复(从onAttach生命周期开始)。 getActivity()空指针 可能你遇到过getActivity()返回null,或者平时运行完好的代码,在“内存重启”之后,调用getActivity()的地方却返

m0_61417918的博客 315

基于Ollama本地大模型的Python自动化翻译工具开发实践

本文详细介绍了如何利用Ollama本地大模型和Python开发自动化批量翻译工具。通过部署本地开源模型、设计精准提示词、处理长文本分割及多种文档格式,构建了一个兼顾隐私安全、可控性与成本效益的翻译流水线,有效解决了文档批量翻译的痛点。

yhn45678901的博客 559

Can not perform this action after onSaveInstanceState

IllegalStateException: Can not perform this action after onSaveInstanceState 可能的原因之一: Fragment 在显示或者隐藏,移除是出现Can not perform this action after onSaveInstanceState #解决办法:onSaveInstanceState方法是在该Activ...

Swer 3万+

使用-flat.vmdk文件恢复ESXI虚拟机的vmdk文件

解决flat.vmdk文件丢失恢复问题

神秘人的博客 6763

魔兽RPG老地图逆向工程:解密三国列传3.0武将招募限制(附JASS实战)

本文详细解析了《三国列传3.0》魔兽RPG地图的武将招募限制机制,通过逆向工程和JASS脚本修改技术,突破SLK加密防线并解密游戏逻辑。文章提供了从工具准备到代码修改的完整实战指南,帮助玩家解除武将数量限制,重温经典游戏的乐趣。

i1j2k的博客 352

从源码角度理解Can not perform this action after onSaveInstanceState异常

在开发中经常遇到Fatal Exception: java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState异常,那这个异常出现原因是什么呢,怎么解决呢? 问题描述 出现Fatal Exception: java.lang.IllegalStateException:Can not pe...

小唯的博客 6081

多智能体强化学习MASAC算法:从原理到PyTorch实战详解

深度强化学习通过智能体与环境的交互试错来学习最优策略,是解决复杂序列决策问题的核心技术。其核心原理在于利用价值函数或策略梯度来最大化累积奖励,在机器人控制、游戏AI等领域展现出巨大技术价值。当面对多个智能体需要协同或竞争的复杂动态环境时,单智能体算法因环境非平稳性而面临挑战,多智能体强化学习应运而生。Multi-Agent Soft Actor-Critic (MASAC) 算法融合了SAC的稳定高效与中心化训练去中心化执行的范式,通过维护中心化Critic网络和独立策略网络,有效解决了多智能体协作中的信用

weixin_30080745的博客 322

解决Activity跳转后弹出DialogFragment报错Can not perform this action after onSaveInstanceState

问题描述 在一个activity中,有定时任务,到时间后弹出一个DialogFragment,如果弹出的时候,已经跳转到了下一个activity,就会报错:Can not perform this action after onSaveInstanceState。 解决方案 在DialogFragment中另写一个show方法: public void showAllowingStateL...

苦逼的程序猿☺ 3865

SwiftUI 5 教程之 SwiftData使用入门(教程含源码)

对我来说,记住日期很难。是的,您可以设置该日期的提醒,但该提醒无法产生紧迫感。让我们创建一个应用程序,我们可以在其中添加将来发生的事件。如果我们可以在主应用程序上看到所有即将发生的活动,那就太好了。也许将它们添加到小部件上以营造紧迫感。对于任何事件,我们需要跟踪哪些事情?我们将在下一篇文章中添加更多属性,其中我们将讨论迁移。现在,这就是我们所需要的。开始吧。

iCloudEnd的博客 980

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

解决IllegalStateException: Can not perform this action after onSaveInstanceState

今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState: E/AndroidRuntime(12747): Caused by: java.lang.IllegalStateException: Can not perform this action

第一菜鸟XMU 11万+

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Fragment中出现Can not perform this action after onSaveInstanceState

前言在项目开发中,将app安装在某些手机上时,有时会出现闪退的情况。通过定位抛出的异常信息如下:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

yuminfeng728的博客 923

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Can not perform this action after onSaveInstanceState 问题的原因与解决

我出现这个问题的原因就是,在当前Acitivity 操作的过程中 跳到另外一个界面做了一些操作然后返回并带了一些参数,再在当前Activity 显示DialogFrament 展示带过来的参数,此时崩溃出现了这个错误信息 IllegalStateException: Can not perform this action after onSaveInstanceState 出现这个问题的原因大致就是在当前Activity 操作的过程中,跳转到其它视图做了一些操作,此时当前Activity运行了onS.

hnzkxlw的专栏 5365

FragmentTransaction commit Can not perform this action after onSaveInstanceState

http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html

冷秋魂的专栏 6151

Android error:Can not perform this action after onSaveInstanceState

最近在项目中发现了这个bug,不是每次都发生,偶然会出现的,抓取的log日志: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.ja

xuewater的专栏 2853
上一篇: Android savedInstanceState的使用
下一篇: 可展开列表组件ExpandableListView解析
colorfulshark
博客等级 码龄15年 631粉丝 459原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值