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---");
}

Android中,您可以使用`onSaveInstanceState()`方法保存Bundle对象来保存和恢复应用程序的状态。要保存Bundle对象,请按照以下步骤进行操作: 1. 重写`onSaveInstanceState()`方法:在您的Activity中,重写`onSaveInstanceState()`方法,以便在Activity被销毁时保存应用程序的状态。 2. 创建Bundle对象:在`onSaveInstanceState()`方法中,创建一个新的Bundle对象,并将应用程序的状态保存在该对象中。您可以使用put方法向Bundle对象添加键值对,以保存数据。 3. 系统保存Bundle对象:当Activity被销毁时,系统会调用`onSaveInstanceState()`方法,并将Bundle对象传递给它。系统会将Bundle对象保存在内存中,以便在Activity重新创建时恢复应用程序的状态。 以下是一个示例,在该示例中,我们将保存一个字符串值和一个整数值: ``` @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("myString", "Hello world"); outState.putInt("myInt", 42); } ``` 在上面的示例中,我们使用putString()方法和putInt()方法向Bundle对象添加键值对。请注意,我们还调用了`super.onSaveInstanceState(outState)`方法,以便系统可以保存任何必要的状态。 在Activity重新创建时,您可以使用`onRestoreInstanceState()`方法从Bundle对象中检索数据。请注意,您还需要在Activity的`onCreate()`方法中检查Bundle对象是否为null。如果Bundle对象不为null,则表示Activity是从先前的状态恢复的,并且您需要从Bundle对象中恢复应用程序状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值