在android中,要少用静态变量。

我现在做的一个应用中,之前的开发人员使用静态变量来存储cookie,这个全局的静态变量用来验证身份。

这时客户反应,应用长时间不使用,再次使用,会提示身份过期。

后来经查,问题基本确定在静态变量上。

上stackoverflow查了android中static变量的生命周期,有人这么说

Lifetime of a static variable: A static variable comes into existence when a class is loaded by the JVM and dies when the class is unloaded,if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens:

1. the class is unloaded
2. the JVM shuts down
3. the process dies

我们应用出现的情况应该就是进程被系统杀掉导致的。

后来这个情况也发现了,就是不断地打开应用,当系统内存不够用时,应用进程会被杀掉。这时再打开应用,就出现了身份过期,也即静态变量为空的情况

静态变量,要慎用!

更多相关文章

  1. 《Android移动应用基础教程》(Android(安卓)Studio)(第二版)黑马教程
  2. Android(安卓)Shape Drawable 静态使用和动态使用(圆角,渐变实现)
  3. C#/mono开发Android应用程序入门(二)-第一个应用程序(Hello World)
  4. Android将胜过Windows Mobile五大原因
  5. android系统体系结构
  6. android介绍以及学习方法
  7. android test 和 instrumentation
  8. Android(安卓)免安装应用
  9. Android简明开发教程九:创建应用程序框架

随机推荐

  1. Android与Windows Socket通信,TLS双向认证
  2. android menu菜单
  3. Android 全自动js脚本测试 Appium教程—
  4. Android(安卓)WebView系列(一)WebView的基
  5. 浅谈Android系统开发中LOG的使用
  6. phonegap导入工程报错解决办法 错误解析a
  7. Android中抓取手机视频流数据
  8. Android APN的设置问题 默认“已起用数据
  9. Android Camera 模块分析(三)
  10. Windows7 Android 开发环境搭建