java 公共静态变量和Android Activity生命周期管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12189476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:56:24  来源:igfitidea点击:

Public static variables and Android activity life cycle management

javaandroidstaticandroid-activityandroid-lifecycle

提问by jsstp24n5

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

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

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.

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

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

  2. 变量“foo”不存在?

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

回答by nandeesh

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

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

回答by agamov

Good explanation can be viewed here from 2:50 http://www.infoq.com/presentations/Android-Design

好的解释可以从 2:50 在这里查看 http://www.infoq.com/presentations/Android-Design

Here are some instructions for those who want to test this issue manually: Create android v.4 emulator, then go to settings -> developer settings -> disable background tasks. Then create sample android project with 2 activities, declare static variable in activity A, initialize it in onCreate() method. Place a button in activity A that starts activity B. In Activity B's onCreate() method print the value of A.staticVar to logcat.

以下是针对想要手动测试此问题的人的一些说明:创建 android v.4 模拟器,然后转到设置 -> 开发人员设置 -> 禁用后台任务。然后创建具有 2 个活动的示例 android 项目,在活动 A 中声明静态变量,在 onCreate() 方法中对其进行初始化。在活动 A 中放置一个启动活动 B 的按钮。在活动 B 的 onCreate() 方法中,将 A.staticVar 的值打印到 logcat。

Launch the project - activity A appears. Hit the button - activity B appears, value of static variable is printed to logcat. Press the home button and launch any other program - your sample project process will be killed (because you have disabled background processes). Now long-press on home button - you will see the list of recently launched programs. Select your sample project - OS will try to recover your project's activities back-stack and recreate last running activity B. But at this step program will crash with NullPointerException because A.staticVar will be null, and we are trying to print it to logcat.

启动项目 - 出现活动 A。点击按钮 - 活动 B 出现,静态变量的值被打印到 logcat。按主页按钮并启动任何其他程序 - 您的示例项目进程将被终止(因为您已禁用后台进程)。现在长按主页按钮 - 您将看到最近启动的程序列表。选择您的示例项目 - 操作系统将尝试恢复您项目的活动堆栈并重新创建上次运行的活动 B。但在这一步程序将因 NullPointerException 而崩溃,因为 A.staticVar 将为空,我们正尝试将其打印到 logcat。

回答by Ted Hopp

The answer is (3). If you need to keep values, persist them in shared preferences when each activity pauses and restore them when it resumes. Alternatively, you can also maintain an "initialized" static flag and re-initialize the static variables from any activity's onCreate()method if it is false.

答案是(3)。如果您需要保留值,请在每个活动暂停时将它们保留在共享首选项中,并在恢复时恢复它们。或者,您也可以维护一个“已初始化”的静态标志,onCreate()如果它为假,则从任何活动的方法重新初始化静态变量。