安卓:在低内存的静态变量空 [英] Android : Static variable null on low memory

查看:210
本文介绍了安卓:在低内存的静态变量空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有一些静态变量的应用程序。 这些变量被存储在名为DataContext的一个独立级。 这些变量从在应用程序启动原始文件进行初始化(一个名为DataContext.initConstant(方法)被称为MyApplication的延伸应用的OnCreate())。

I have an application which has some static variables. These variables are stored in an independent Class named DataContext. These variables are initialized from raw files at the application start (a method named DataContext.initConstant() is called in the onCreate() of MyApplication which extends Application).

(编辑:initConstant方法使用的AsyncTask来从文件加载这个数据)。

(EDIT : the initConstant method use an AsyncTask to load this data from files).

在我的应用程序涉及到后台一定时间或当用多少内存我的应用程序,这些静态变量成为空。

When my application comes to the background for a certain time or when my application used to much memory, these static variables become null.

1 - 我怎么能prevent呢?

1- How could I prevent it ?

2 - 如果不是我应该做我的静态变量?

2- if not what should I do with my static variables ?

予有跨越不同的活动中使用它们存储在静态变量的其他数据,但是我清除它们或它们传递MyApplication的向空中onLowMemory()。

I have other data which are stored in static variables to be used across different activities, but I clear them or pass them to null in the onLowMemory() of MyApplication.

3什么是保持一些DATAS访问活动之间,如果这些的数据都是以大的一个意图被序列化,不能使用数据库的一个原因的最佳途径,而无法通过序列化存储在文件呢?

3- What is the best way to keep some datas accessible between activities if these datas are to big to be serialized in an Intent, can't use a database for a reason, and can't be stored in files through serialization too ?

谢谢您的回答/意见

推荐答案

最有可能的问题是,您的应用程序被杀害,而它在后台,然后重新当你回来吧。查看活动生命周期文档可能发生这种情况的上单一的活动。你需要确保你在正确的时间点移动任何东西存储在内存中更永久存储,以避免丢失信息,如果该应用程序就会被杀死。

Most likely the issue is that your application is being killed while it is in the background, and then recreated when you come back to it. Check out the Activity Lifecycle documentation on when this might occur for a single activity. You need to make sure that you move anything stored in memory to more permanent storage at the correct point in time to avoid losing that information if the app gets killed.

我不知道究竟要存储,但它听起来像使用共享preferences可能会工作得很好。此页面上数据存储介绍了一些更不同的方式永久存储的数据,包括共享preferences。

I'm not sure what exactly you are storing, but it sounds like using Shared Preferences might work well. This page on Data Storage explains a number of different ways of more permanently storing data, including Shared Preferences.

这篇关于安卓:在低内存的静态变量空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆