使用全局变量的应用类全局变量

2023-09-06 04:45:00 作者:ゞ你不想要、我也懒得给

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

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.

这是你需要了解如何使用应用程序对象时,另一件事是它的生命周期。不幸的是,这种讨论是非常主观的,但幸运的是,如讨论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

祝你好运!