简体   繁体   English

静态类,通过多个活动保存信息

[英]A Static Class to hold information through multiple activities

I am building an app with a pretty decent UI, and thus has multiple activities and classes. 我正在构建具有相当不错的UI的应用程序,因此具有多个活动和类。 Most of the classes are relative to a single activity or single other class, how ever: I need to create a "class" that can store/pass information and perform functions from multiple other activites and classes. 大多数类都与单个活动或其他单个类有关,但是,我是这样:我需要创建一个“类”,该类可以存储/传递信息并执行来自多个其他活动和类的功能。

I am worried that when I create a new object of the "class" in each other activity/class that it will erase or call a new instance for this "class", where as I need the data to stay put while I go from activity to activity, to be able to be called upon and used/changed, and be acessible throughout the application. 我担心当我在每个活动/类中创建一个新的“类”对象时,它会擦除​​或调用该“类”的新实例,因为我需要在离开活动时保留数据活动,可以被调用和使用/更改,并且在整个应用程序中都是必需的。

Is there any special way to do this? 有什么特别的方法吗?

Thank you. 谢谢。

您可以使用sharedpreferences。如果没有太多变量,则创建一个包含所有内容的类

public static "variables";

Do not implement the singleton design pattern; 不要实现单例设计模式; this is the reason for listeners. 这就是听众的原因。 A UI component registers a bunch of listeners (ie model and/or controller objects). UI组件注册了一堆侦听器(即模型和/或控制器对象)。 When an event occurs, listeners are notified and update their state accordingly. 事件发生时,将通知侦听器并相应地更新其状态。 There is no reason to delegate the handling of messages to a proxy object, such as the singleton in this case. 没有理由将消息的处理委托给代理对象,例如本例中的单例。

Singleton pattern: Drawbacks 单例模式:缺点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM