在Android中使用静态变量

发布于 2024-08-25 23:29:06 字数 274 浏览 11 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

很酷又爱笑 2024-09-01 23:29:06

static 字段作为一个整体附加到 Class 实例上,而该实例又附加到加载该类的 ClassLoader 上。当整个 ClassLoader 被回收时,the_instance 将被卸载。我 90% 确信当 Android 销毁应用程序时(不是当它进入后台或暂停,而是完全关闭时)会发生这种情况。

因此,只要您的应用程序运行,它就一直存在。单例是个好主意吗?人们有不同的看法。我个人认为,只要使用得当,就可以了。我认为 Android 上的答案不会有太大变化。内存使用本身并不是问题,而是问题所在。如果您需要在内存中加载一堆内容,那么无论您是否将数据封装在单例中,这要么是一个问题,要么不是一个问题。

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.)

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.

芸娘子的小脾气 2024-09-01 23:29:06

我认为静态变量还可以。

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.

霊感 2024-09-01 23:29:06

与其他人所说的相反 - 这还不错。当然,它有一定的结构。在官方 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.

转瞬即逝 2024-09-01 23:29:06

我不确定这种方法是否适用于可用内存有限的移动平台。更不用说该应用程序将在支持多任务的设备上运行。

我认为,这种方法可能会占用设备的内存,但我没有文件支持这一点。也许比我受过更多教育的人可以分享他们的想法。

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.

╄→承喏 2024-09-01 23:29:06

不,不要这样做! Singleton 是一种反模式!。相反,请使用依赖项注入,无论是通过框架(例如通过 DaggerRoboguice)或通过显式传递实例化对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文