它是不好的做法,以保持数据的静态变量? [英] Is it bad practice to keep data in static variables?

查看:133
本文介绍了它是不好的做法,以保持数据的静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这个确切的问题很可能被问过。通过搜索我找不到它。如果你发现一个重复的标志吧。)

在一个Android应用程序,它是不好的做法,在这些情况下,静态字段存储对象?

  1. 应用程序数据。是不是坏保持静态变量的应用程序数据类的应用程序运行时?目前我储存在我的应用程序类的一个实例变量中的数据。这时需要的数据类可以从该应用程序
  2. 数据
  3. 上下文的等等。它是不好的做法来存储上下文(例如凡提述活动应用程序)的静态字段?这可以在需要例如一类可使用一个 LayoutInflater 或资源。目前,我通过了上下文 s到方法需要它们作为参数。<​​/ li>
解决方案

是,是。 :)

静态字段。有很多的问题,静态字段的过度使用。他们不仅是通过一个有趣的保证金慢访问,也是他们很容易被安卓毁于一旦,而且它通常哈克检查它们的引用所有的地方,或填写您的getter / setter方法​​与IF(sSomeStatic == NULL ){返回新SomeStatic()}。这是确定存储的静态引用一类称为(例如)的ApplicationData您存储一些值,嘿,我们需要一些全局飘飞,但它是如此容易滥用它,我皱眉我每次检查新的Andr​​oid开发者源$ C ​​$ C。

是的,存储你的应用程序实例Singleton模式,并使用它,但不要加200静态字段到你的应用程序实现,因为你可以做YOURAPP.getInstance()SomeLazyValueYouAddedHere();

这是不好的。它会导致不好的做法,它会比有,你访问硬盘参考设计好慢。

我可以永远继续下去,但也有很多的StackOverflow讨论(一些激烈!)这件事。如果你在这里,我假设你要求的工作经验;我一直在做的Andr​​oid用户可以在不同的项目几年,我的经验一直是少静,越多越好。

现在的背景下 ...哦语境。请勿储存在一个硬引用的语境下,直到永远。否则你会泄漏内存。一个活动引用了查看和许多其他的东西。如果存储的背景下,要存储的活动,事情变坏从那里。学习是传递的情况下,使用应用程序上下文只要有可能,如果你需要的是传递,这样做有很好的理由。大部分时间在App上下文是足够用于获取资源,字符串等 如果你要存储的背景下,始终将context.getApplicationContext();切勿将静态的活动环境。你可以在google这也和计算器有一些很好的答案。

如果你能负担得起一个且只有一个Android的书,拿到 BNR 的之一。即使寿的Andr​​oid可能会发布新的SDK飘飞,这些概念是完全有效的模式笔者使用是正确的方式来处理活动,上下文,片段等。

更新您的应用程序应该是这样的:

 公共类YourApp扩展应用{
   私有静态YourApp sInstance;
   公共YourApp(){
      超();
      sInstance =这一点;
   }
   公共静态YourApp的getInstance(){
      返回sInstance;
   }
}
 

而在这种情况下,是的,你所得到的相同的静态引用相同的应用程序上下文。

(This exact question may very well be asked before. I couldn't find it via searching. If you find a duplicate flag it.)

In an Android application, is it bad practice to store objects in static fields in these cases?

  1. Application data. Is it bad to keep application data in static variables in a class while the application is running? Currently I'm storing the data in an instance variable in my Application class. Then classes that need the data can obtain the data from the Application.
  2. Context's etc. Is it bad practice to store a Context (e.g. a reference to an Activity or an Application) in a static field? This can be used in a class that needs e.g. a LayoutInflater or resources. Currently, I am passing the Contexts to methods needing them as arguments.

解决方案

Yes and Yes. :)

Static Fields. There are a lot of problems with excessive usage of Static fields. Not only they are slower to access by an interesting margin, but also they are prone to be destroyed overnight by Android, and it's usually hacky to check for their references all over the place or fill your getter/setters with if (sSomeStatic == null) { return new SomeStatic()}. It's ok to store a static reference to a class called (for example) ApplicationData where you store some values, hey, we NEED some globals every now and then, but it's so easy to abuse it, that I frown every time I inspect new Android devs' source code.

Yes, store your Application instance in a singleton pattern and use it, but don't add 200 static fields to your Application implementation just because you can do YOURAPP.getInstance().SomeLazyValueYouAddedHere();

That is bad. It leads to bad practices and it will be slower than having a good design where you access hard references.

I could go on forever but there are a lot of StackOverflow discussions (some heated!) about this. If you are here, I'm assuming you're asking for experience; I've been doing Android for a couple of years in different projects and my experience has always been that the less Static, the merrier.

Now the context… oh the Context. Don't store the Context in a hard reference, ever. Or you will leak memory. An activity has references to View and a multitude of other things. If you store the Context, you're storing the activity and things go bad from there. Learn to pass the Context around, use the Application Context whenever possible and if you need to pass it around, do it for very good reasons. Most of the time the App context is enough for getting resources, strings, etc. If you're going to store the Context, always store context.getApplicationContext(); Never store a static activity context. You can google this too and StackOverflow has some good answers.

If you can afford one and only one Android book, get the BNR one. Even tho Android may release new SDKs every now and then, the concepts are completely valid and the patterns the author uses are the right way to deal with Activities, Contexts, Fragments, etc.

UPDATE Your Application should look like this:

public class YourApp extends Application {
   private static YourApp sInstance;
   public YourApp() {
      super();
      sInstance = this;
   }
   public static YourApp getInstance() {
      return sInstance;
   }
}

And in that case, yes, you are getting the same Static reference to the same App Context.

这篇关于它是不好的做法,以保持数据的静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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