使用全局变量的应用类 [英] Use Application class for global variables

查看:132
本文介绍了使用全局变量的应用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么理由不使用应用程序的类共享整个活动的变量?

Is there any reason not to use the Application class to share variables across Activities?

有关实例的句柄到数据库或单一的HttpClient。

For instance a handle to the DB or a single HttpClient.

推荐答案

根据官方的文档,你可以用它来维持全球应用程序状态。国际海事组织,你不会想守住太多重物,全球范围内。 Ofcourse,太多太模糊:)有共享(持久)数据等方式:preferences,数据库,文件。您可能要检查是否有这些都是您的问题更合适。

According to the official documentation "you can use it to maintain global application state". IMO you wouldn't want to hold onto to too many heavy objects, globally. Ofcourse, "too many" is too fuzzy:) There are other ways of sharing (persistent) data: Preferences, database, files. You might want to check whether any of these are a better fit for your problem.

这是你需要了解如何使用应用程序对象时,另一件事是它的生命周期。不幸的是,这种讨论是非常主观的,但幸运的是,如讨论<一个href="http://groups.google.com/group/android-developers/browse%5Fthread/thread/502fe58d8ed44102/1fce6026591d083b?lnk=gst&q=Application+object#1fce6026591d083b">has已经完成

The other thing that you need to understand about when using Application object is its Life cycle. Unfortunately, discussion of that can be very subjective but fortunately, such as discussion has already been done

祝你好运!

这篇关于使用全局变量的应用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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