Posted on by Ram Kulkarni

In one of my Android application, I was storing some data in a static member variable. After few hours of inactivity, when I used to open the application, I would see that all the data I had stored in the static member variable was gone.

I know about the Android Activity life cycle and that Android can unload Activity class if it is not active. However I did not think that it would reset all static members too. So I had three options to persist data -

  1. Store it in preferences
  2. In a file (local or on external media)
  3. In the database
  4. In the Android application class

See this articlefor more details about data storage options in Android. Since the data is not large and I did not already have Android database created for this app (this app does use database, but it’s HTML5 database), I decided to use option #4. Storing in application class also suited this application because I do not need the data if Android or user kills the application.

So, be very careful when using static variables in Activity class.

-Ram Kulkarni

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 数据分析的基石-真实世界
  2. 降级?限流?程序员双十一过后如何5元花3天?
  3. 设计思想赏析-分布式id生成算法-雪花算法
  4. 设计思想赏析-基因算法
  5. 第一课 vscode软件和常用插件的下载安装
  6. 编辑器安装与emmet语法
  7. Visual Studio Code 编辑器
  8. 超强大工具!快速下载安装vscode和必备插件
  9. 硬盘提示函数不正确怎么找回
  10. 菜鸟第一步