Java 在 Android 中使用静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2475978/
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
Using static variables in Android
提问by michael
In android, are using static variables a recommended practice? E.g, implementing a Singleton pattern in Java, I usually do:
在 android 中,是否推荐使用静态变量?例如,在 Java 中实现单例模式,我通常会这样做:
private static A the_instance;
public static A getInstance() {
if (the_instance == null) {
the_instance = new A();
}
return the_instance;
}
Also, when does this get cleaned up by the Android JVM?
另外,Android JVM 什么时候会清理它?
Thank you.
谢谢你。
采纳答案by Sean Owen
static
fields are attached to the Class
instance as a whole, which is in turn attached to the ClassLoader
which loaded the class. the_instance
would be unloaded when the entire ClassLoader
is reclaimed. I am 90% sure this happens when Android destroys the app (not when it goes into the background, or pauses, but is completely shut down.)
static
字段Class
作为一个整体附加到实例,而实例又附加到ClassLoader
加载类的 。the_instance
当整个ClassLoader
被回收时将被卸载。我 90% 确定当 Android 销毁应用程序时会发生这种情况(不是当它进入后台或暂停时,而是完全关闭时。)
So, think of it as living as long as your app runs. Is Singleton a good idea? People have different views. I think it's fine when used appropriately, myself. I don't think the answer changes much on Android. Memory usage isn't the issue per se; if you need to load a bunch of stuff in memory, that's either a problem or it isn't, regardless of whether you encapsulate the data in a Singleton.
因此,只要您的应用程序运行,就可以将其视为存在。单身人士是个好主意吗?人们有不同的看法。我个人认为使用得当就好。我认为答案在 Android 上没有太大变化。内存使用本身不是问题;如果您需要在内存中加载一堆东西,那要么是问题,要么不是问题,无论您是否将数据封装在单例中。
回答by RobGThai
I'm not sure if such approach is good for mobile platform where you have limited memory available to you. Not to mention that the application will be run on a multi-tasking enabled device.
我不确定这种方法是否适用于您可用内存有限的移动平台。更不用说该应用程序将在支持多任务的设备上运行。
I think, this approach may hog memory from the device but I have no document to support this. Perhaps someone who's more educated than me can share their thoughts.
我认为,这种方法可能会占用设备的内存,但我没有文件支持这一点。也许比我受过更多教育的人可以分享他们的想法。
回答by Michael Aaron Safyan
回答by user256239
I think static variables are OK.
我认为静态变量没问题。
This is what Android doc says:
这是 Android 文档所说的:
http://developer.android.com/guide/appendix/faq/framework.html
http://developer.android.com/guide/appendix/faq/framework.html
How do I pass data between Activities/Services within a single application?
如何在单个应用程序中的活动/服务之间传递数据?
A public static field/method
公共静态字段/方法
An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.
使数据可跨活动/服务访问的另一种方法是使用公共静态字段和/或方法。您可以从应用程序中的任何其他类访问这些静态字段。为了共享一个对象,创建你的对象的活动设置了一个指向这个对象的静态字段,而任何其他想要使用这个对象的活动只需要访问这个静态字段。
回答by Mikel
Contrary to what other people say - it is more than ok. Granted, it has some structure to it. In the official googlesamples/android-architecture repo it is used under todo-mvp-clean (Todo app implementing MVP pattern and following Clean Architecture principles). Check out this file.
与其他人所说的相反 - 这不仅仅是好的。当然,它有一些结构。在官方的 googlesamples/android-architecture repo 中,它在 todo-mvp-clean(实现 MVP 模式并遵循 Clean Architecture 原则的 Todo 应用程序)下使用。看看这个文件。
What you can see is a lot of static methods referencing singleton getters.
您可以看到许多引用单例 getter 的静态方法。
Modern, less error prone and convenient alternative is the Dagger DI framework.
现代的、不易出错且方便的替代方案是 Dagger DI 框架。