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

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

问题描述

根据文档,Android 操作系统可以终止后台堆栈后面的活动.

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

例如,假设我有一个应用程序并打开主活动(我们称之为活动 A).在这个公共活动类中,我声明并初始化了一个公共静态变量(我们称之为foo").在活动 A 的 onCreate() 方法中,我更改了foo"的值.用户从活动 A 开始我的应用程序中称为活动 B 的另一个活动.活动 B 中使用变量foo".然后在用户导航到其他应用程序中的其他活动后暂停活动 B.最终,在发生内存短缺后,Activity A 和 Activity B 可以被杀死.在用户导航回我的应用程序后,它会重新启动(实际上是重新创建")活动 B.

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.

发生了什么:

  1. 此时变量foo"是否具有在活动 A 的 onCreate() 方法运行时为其设置的值?

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

变量foo"不存在?

变量foo"存在,但现在是初始化值而不是活动 A 的 onCreate() 方法中设置的值?

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.

因此您在活动 A 中设置的任何值都不会持久

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

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

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