公共静态变量和Android活动的生命周期管理 [英] Public static variables and Android activity life cycle management

查看:165
本文介绍了公共静态变量和Android活动的生命周期管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档的Andr​​oid操作系统可杀死在backstack后部的活动。

所以,比方说我有一个应用程序,并打开了主要活动(姑且称之为活性A)。在本次公开活动课我声明并初始化一个公共静态变量(姑且称之为富)。在一个活动的onCreate()方法,然后我改变的价值富。从活动一用户开始在我的应用程序的另一项活动叫做活动B.变富是用在活动B.活动B被暂停,然后在用户导航到其他应用程序的一些其他活动。最终,内存不足发生后,活动一个接着b活动可以被杀死。在用户导航回我的应用程序将重新启动(实际上是再现)活动B.

会发生什么:

  1. 威尔变富在这一点上有设置为它的价值,当一个活动的onCreate()方法运行?

  2. 变富不存在?

  3. 变量foo的存在,但现在是初始化值,而不是价值的一个活动的onCreate()方法设置?

解决方案

如果该进程被终止,则所有的静态变量将被初始化为默认值。

所以,无论你的价值在活动设置了将不会保留

According to the documentation the Android OS can kill the activity at the rear of the backstack.

So, say for example I have an app and open the Main Activity (let's call it Activity A). In this public activity class I declare and initialize a public static variable (let's call it "foo"). In Activity A's onCreate() method I then change the value of "foo." From Activity A the user starts another activity within my app called Activity B. Variable "foo" is used in Activity B. Activity B is then paused after the user navigates to some other activities in other apps. Eventually, after a memory shortage occurs, Activity A then Activity B can be killed. After the user navigates back to my app it restarts (actually "recreates") activity B.

What happens:

  1. Will variable "foo" at this point have the value that was set to it when Activity A's onCreate() method ran?

  2. Variable "foo" does not exist ?

  3. Variable "foo" exists and but is now the initialized value and not the value set in Activity A's onCreate() method ?

解决方案

If the process is killed then all static variables will be reinitialized to their default values.

So whatever value you have set in Activity A will not persist

这篇关于公共静态变量和Android活动的生命周期管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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