可以静态变量的值由系统在Android应用程序进行调零? [英] Can static variable value be nulled by system in android app?

查看:131
本文介绍了可以静态变量的值由系统在Android应用程序进行调零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当机器人操作系统关闭,不能在活动堆栈顶部的活动,我的一些静态变量的变空。我绝对相信,这些变量指向的对象之前,我并没有自己改变自己的价值。

I have noticed, that when android OS closes activities which are not in the top of activity stack, some of my static variables become null. I'm absolutely sure, the variables pointed to objects before and that I did not change their value by myself.

休闲活动后,我得到NullPointerException异常的原因我的静态变量之一(这是在应用程序的子类的onCreate初始化,应该是在流程生命周期中的任何时间NOT NULL)为null。

After activity recreation I get nullPointerException cause one of my static variable (which is initialized in Application's subclass onCreate and is supposed to be not null at any time of process lifecycle) is null.

由于它不是我是谁空值的变量,我想这是Android操作系统而关闭后台活动和空静态变量由于内存不足。
这可能吗?

Since its not me who nulls the variable, I suppose it is android OS which closes background activities and nulls static variables due to lack of memory. Is it possible?

推荐答案

Android不密切活动,不在活动堆栈的顶部。如果您的应用程序转到后台和Android决定它想要收回它只是内存杀死承载你的活动的过程。当用户返回到应用程序,Android的创建一个新的进程,并重新创建的活动,这是对活动的栈顶

Android does not close activities which are not on top of the activity stack. If your application goes to the background and Android decides that it wants to reclaim the memory it just kills the process that hosts your activities. When the user returns to the application, Android creates a new process and recreates the activity that was on the top of the activity stack.

在大多数的概率,这就是你所看到的。显然,如果你的进程被杀掉并重新创建,您的静态变量将是无效的。

In most probability, that's what you are seeing. Obviously if your process is killed and recreated, your static variables will be null.

这篇关于可以静态变量的值由系统在Android应用程序进行调零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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