Android的 - 在保持全球数据? [英] Android - where to keep global data?

查看:101
本文介绍了Android的 - 在保持全球数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想从我的应用程序中的多个位置,将从共享preferences加载访问多个二维和三维数组的字符串时,我的应用程序启动和保存回共享preferences 每当的onPause()被称为上的任何活动。这太繁琐与意图或捆绑活动之间传递它们。有没有什么办法将数据存储在全球范围内的Andr​​oid?

I have several 2D and 3D arrays that I'd like to access from multiple places within my app, which will be loaded from SharedPreferences as strings when my app starts and saved back to SharedPreferences whenever onPause() is called on any activity. It's too tedious to pass them between activities with intents or bundles. Is there any way to store data globally in Android?

推荐答案

我们所做的,是用来使用大多是静态字段和静态方法,虽然不是所有的都是静态的一个Globals类。这些大多是可以由在应用许多不同的类/活动中使用的字段和方法。这个问题是要不允许它生长失控。每过一段时间中,我经历,看看有什么可以取出来,放在一起上课或移动到现有类。有时,在你还需要应用程序上下文,我已经通过传递上下文在某些情况下还是创造,获取应用程序的快捷的方法解决了这个问题。可能有内存泄漏问题,但我没有任何问题,迄今为止,虽然这并不意味着我不会的。

What we have done, is used a Globals class that uses mostly static fields and static methods, though not all are static. These are mostly fields and methods that may be used by many different classes/activities in the app. The problem with this is to not allow it to grow out of control. Every once in awhile, I go through and see what can be taken out and put in classes together or moved to an existing class. Sometimes there is the problem in which you need application context also which I have resolved by passing context in certain situations or creating a method that gets the application's context. There may be problems with memory leaks but I have not had any issues thus far, although that doesn't mean I won't.

有人告诉我不要做这做那它是不是OOP,但我认为这是不正确,也没有人告诉我,为什么这是错的呢。你还创建对象,并按照面向对象的标准。很高兴我能帮助

I have been told not to do this and that it isn't OOP but I think that is incorrect and no one has told me why this is wrong yet. You still create objects and follow OOP standards. Glad I could help

这篇关于Android的 - 在保持全球数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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