android基础之onSaveInstanceState用法(一)保存容易被回收的自定义类的静态全局变量
来源:互联网 发布:前端解析excel成json 编辑:程序博客网 时间:2024/06/05 16:54
当切换到其它apk时且内存不足时,在之前设置的单例模式静态变量会被回收掉。解决方法要么定义为Applicantion全局变量,要么在onSaveInstanceState中保存
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
//恢复容易被回收的投保人
if(savedInstanceState!=null){
//自定义单例
InsuranceFormAgent insFormAgent = InsuranceFormAgent.createInstance();CustomerDetailInfo applicant = (CustomerDetailInfo) savedInstanceState.getSerializable("Applicant");
if(applicant!=null){
insFormAgent.setApplicantInfo(applicant);
}
AppLog.debug("-------ActProposalChooseProduct---------onCreate get the savedInstanceState---------");
}
setContentView(R.layout.demo_pro_choose_insurant);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// 为了防止万一程序被销毁的风险,这个方法可以保证重要数据的正确性
// 不写这个方法并不意味着一定出错,但是一旦遇到了一些非常奇怪的数据问题的时候
// 可以看看是不是由于某些重要的数据没有保存,在程序被销毁时被重置
// Save away the original text, so we still have it if the activity needs to be killed while paused.
//切换到其它apk时,当内存不足时,在之前设置的静态变量投保人会被回收掉。所以在此要保存
InsuranceFormAgent insFormAgent = InsuranceFormAgent.createInstance();
CustomerDetailInfo customer = insFormAgent.getmApplicantInfo();
savedInstanceState.putSerializable(APPLICANT,customer);
super.onSaveInstanceState(savedInstanceState);
AppLog.debug("-----------ActDemoProChooseInsurant---------------onSaveInstanceState--");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// int mCount = savedInstanceState.getInt("IntTest");
//恢复容易被回收的投保人
if(savedInstanceState!=null){
InsuranceFormAgent insFormAgent = InsuranceFormAgent.createInstance();
CustomerDetailInfo applicant = (CustomerDetailInfo) savedInstanceState.getSerializable(APPLICANT);
if(applicant!=null){
insFormAgent.setApplicantInfo(applicant);
}
}
AppLog.debug("------ActDemoProChooseInsurant----------onRestoreInstanceState---");
}
- android基础之onSaveInstanceState用法(一)保存容易被回收的自定义类的静态全局变量
- android基础之onSaveInstanceState用法(二)
- 28.使用onSaveInstanceState()保存活动被回收时的数据
- onSaveInstanceState()解决活动被回收时临时数据得不到保存的问题
- Android中onSaveInstanceState()数据的保存
- Android学习基础之onSaveInstanceState和onRestoreInstanceState触发的时机
- Android之Activity(八):活动意外被回收,如何处理临时数据的保存问题?
- Android的onSaveInstanceState和onRestoreInstanceState用法
- android之保存全局变量
- android之保存全局变量
- Android中application全局变量被系统回收的问题
- Android使用static定义全局变量被系统回收的问题
- Android中全局变量被系统回收的问题
- android保存数据(意外被清理的情况下)和android的生命周期(onRestoreInstanceState和onSaveInstanceState)学习
- 后台的Activity被系统回收怎么办:onSaveInstanceState
- Activity被回收怎么办?带你解析onSaveInstanceState的源码
- Android--状态保存--onSaveInstanceState和onRestoreInstanceState的作用
- android onSaveInstanceState方法 横坚屏切换的状态保存
- 一些不错的开源库
- jsp 标签
- android中notification使用方法
- memmove and memcpy
- 谈谈SEO未来的发展前景
- android基础之onSaveInstanceState用法(一)保存容易被回收的自定义类的静态全局变量
- iOS之沙盒目录简单介绍
- java与模式 博客园
- linux gcc版本升级4.8.2 string版本太旧问题解决方法
- VMware linux 虚拟机 使用putty
- 博客日记第一天
- Leetcode || Remove Duplicates from Sorted Array
- Hibernate学习总结02——ORM
- order by 使用技巧