在Android中使用静态变量
在android中,使用静态变量是推荐的做法吗? 例如,在 Java 中实现单例模式,我通常会这样做:
private static A the_instance;
public static A getInstance() {
if (the_instance == null) {
the_instance = new A();
}
return the_instance;
}
另外,Android JVM 什么时候会清理它?
In android, are using static variables a recommended practice?
E.g, implementing a Singleton pattern in Java, I usually do:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
static
字段作为一个整体附加到Class
实例上,而该实例又附加到加载该类的ClassLoader
上。当整个ClassLoader
被回收时,the_instance
将被卸载。我 90% 确信当 Android 销毁应用程序时(不是当它进入后台或暂停,而是完全关闭时)会发生这种情况。因此,只要您的应用程序运行,它就一直存在。单例是个好主意吗?人们有不同的看法。我个人认为,只要使用得当,就可以了。我认为 Android 上的答案不会有太大变化。内存使用本身并不是问题,而是问题所在。如果您需要在内存中加载一堆内容,那么无论您是否将数据封装在单例中,这要么是一个问题,要么不是一个问题。
static
fields are attached to theClass
instance as a whole, which is in turn attached to theClassLoader
which loaded the class.the_instance
would be unloaded when the entireClassLoader
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.)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 文档是这样说的:
http://developer.android.com/guide/ appendix/faq/framework.html
如何在单个应用程序内的活动/服务之间传递数据?
公共静态字段/方法
跨活动/服务访问数据的另一种方法是使用公共静态字段和/或方法。您可以从应用程序中的任何其他类访问这些静态字段。要共享对象,创建对象的活动会设置一个静态字段来指向该对象,并且任何其他想要使用该对象的活动只需访问该静态字段。
I think static variables are OK.
This is what Android doc says:
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.
与其他人所说的相反 - 这还不错。当然,它有一定的结构。在官方 googlesamples/android-architecture 存储库中,它在 todo-mvp-clean 下使用(Todo 应用程序实现 MVP 模式并遵循 Clean Architecture 原则)。
看看这个文件.
您可以看到很多引用单例 getter 的静态方法。
现代、不易出错且方便的替代方案是 Dagger DI 框架。
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.
What you can see is a lot of static methods referencing singleton getters.
Modern, less error prone and convenient alternative is the Dagger DI framework.
我不确定这种方法是否适用于可用内存有限的移动平台。更不用说该应用程序将在支持多任务的设备上运行。
我认为,这种方法可能会占用设备的内存,但我没有文件支持这一点。也许比我受过更多教育的人可以分享他们的想法。
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.
不,不要这样做! Singleton 是一种反模式!。相反,请使用依赖项注入,无论是通过框架(例如通过 Dagger 或 Roboguice)或通过显式传递实例化对象。
No. Don't do it! Singleton is an anti-patern!. Instead, use dependency injection, whether via a framework (such as via Dagger or Roboguice) or by explicitly passing the instantiated object.