最近接手一个项目,是别人开发差不多的负责维护。发现一个bug,就是长时间挂后台待机再次进去会”白屏“。或者在低端机开大量程序后也会出现。只能退出程序重新登录。
后来经过排查,发现正是保存登录信息的静态变量被回收。软件有登录功能,登录后获取一些帐号信息,写了一个类专门存放用static变量保存这些信息。
网上说的移到application发现不现实,工作量太大太复杂,后来发现activity有这个方法onSaveInstanceState(Bundle outState)!内存不足被回收时候会被调用在这里做保存就行,然后切回界面onRestoreInstanceState会被执行,这里取回数据。我在BaseActivity重写了操作白屏基本不出现。
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("aa", MyActivityManager.aa);
outState.putString("bb", MyActivityManager.bb);
outState.putString("username", MyActivityManager.username);
// Log.i("OK","onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
MyActivityManager.aa=savedInstanceState.getString("aa");
MyActivityManager.bb=savedInstanceState.getString("bb");
MyActivityManager.username=savedInstanceState.getString("username");
// Log.i("OK","onRestoreInstanceState");
}