android回收内存保存变量,android基础之onSaveInstanceState用法(一)保存容易被回收的自定义类的静态全局变量...

当切换到其它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---"); }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值