Android 在多个应用程序之间共享数据(状态、值、配置)活动

发布于 2024-10-18 00:51:34 字数 594 浏览 7 评论 0原文

您好,我们正在开发一个涉及多个屏幕的复杂应用程序。 我们决定将通用活动保留在单独的应用程序 apk(commonScreen apk)中,以便我们自己以及使用意图的其他人都可以使用这些活动。

现在,我们需要一种通用的方法来存储和共享主应用程序和 commonScreen apk 中存在的活动之间的数据。

我们想到将数据存储在Application对象中(扩展Application) 但是,当我们调用 startActivityForResult(commonScreenApkIntent) 时, onActivityResult,我们可以看到Application中存在的数据消失了(null) 有关使用问题的信息,请参阅不要在应用程序中存储全局数据

当涉及大数据时,使用首选项存储和检索每个活动中的数据是一个好方法吗?

请注意:我们要存储的数据量较多,基本上是应用程序按钮状态(禁用/启用)、值和配置。它可能最多可达 1000 个值。

您能建议一种共享数据的方法吗?

Hi We are developing a complex application which involves multiple screens.
We decided to keep the common activities in a separate applications apk ( commonScreen apk), so that these can be used by ourselves as well as others using intents.

Now, we need a common way to store and share the data between the activities present in our main application and commonScreen apk.

We thought of storing data in Application object ( extend Application)
But, when we call startActivityForResult(commonScreenApkIntent),
onActivityResult, we can see that the data present in Application is gone (null)
see Dont store global data in application for issues using them.

Is storing and retrieving the data in each activity using preference is a good method when large data is involved?

Please note : the amount of data we want to store are more, which are basically application buttons state ( disabled/enabled), value and configurations. It might go up to 1000 values.

Can you please suggest a way to share the data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天暗了我发光 2024-10-25 00:51:34

您是否考虑过使用 SQLite ?,ContentProviders 可以让您的生活更轻松

Have you considered using SQLite ?, ContentProviders can make your life easier

﹉夏雨初晴づ 2024-10-25 00:51:34

我强烈鼓励您重新考虑应用程序的设计。单独的 .apk 中的代码通常应该能够独立运行,不会与数据紧密耦合。如果这只是一个应用程序,并且为了某种方便而将其拆分为 .apk,那么您将会给自己带来各种其他问题 - 例如,如果用户卸载其中一个 .apk,清除其中的数据怎么办?其中之一,等等...?

如果您正在考虑使用 MODE_WORLD_READABLE,请停止并重新考虑。首先,这不是允许设备上安装的任何应用程序读取该数据。它通常还表明存在设计问题,应用程序之间存在不良依赖性,这会困扰您。

I would strongly encourage you to reconsider this design of your app. Code in separate .apks should generally be able to run independently, not have close couplings with data. If this is all one app and it is split into .apks for some kind of convenience, you are going to cause yourself all kinds of other problems -- for example, what if the user uninstalls one of the .apks, clears the data on one of them, etc...?

If you are thinking about using MODE_WORLD_READABLE, stop and reconsider. First not that this is allowing any application installed on the device to read that data. It also generally indicates a design issue with a bad dependency between apps that will bite you.

盗琴音 2024-10-25 00:51:34

我同意hackbod所说的。
(Dhamodharan 只是宣传他的网站。)

您的问题“当涉及大数据时,使用首选项存储和检索每个活动中的数据是一个好方法吗?”
我的答案=否。应优先使用小型原始数据(即=班上每个学生的成绩)。
SharedPreferences 将结果存储在应用程序内的 XML 文件中。如果你想找到 XML 1) 运行你的应用程序。 2) 进入 DDMS 3) 使用“文件资源管理器”,进入目录 Data->数据->你的包名称 (com.example.whatever) ->共享首选项。在那里你应该看到一个 xml 文档。

您的存储选项 http://developer.android.com/guide/topics/ data/data-storage.html:
共享首选项= 将私有原始数据存储在键值对中。
内部存储= 在设备内存上存储私有数据。
外部存储= 将公共数据存储在共享外部存储上。
SQLite 数据库= 将结构化数据存储在私有数据库中。
网络连接= 使用您自己的网络服务器将数据存储在网络上。

我建议你使用 SQLite 存储数据。这样你就可以使用谷歌庞大的数据中心。如果您有自己的服务器,则使用网络连接。外部存储是我的第三个建议,因为外部存储器(SD 卡)往往比内部存储器有更多的空间(内部存储器=内部存储或共享首选项),特别是在低端手机上。例如,我有一个三星 Gio。内部存储器为181MB。 SD 卡容量接近 2 GB。差别很大!

如果您这样做是因为您有“多个屏幕”,那么您应该研究片段。片段允许您在多种屏幕尺寸(即手机和平板电脑)上控制一个应用程序。

听起来你好像一切都倒退了。通用 Activity 应该有权控制 Intent 的调用,而不是相反。您应该有一个通用的 Activity 来控制根据设备显示哪些片段。然后在每个单独的片段对象内,您可以使用 SQLite 检索保存的状态。

I agree with what hackbod said.
(Dhamodharan is just promoting his website.)

Your Question “Is storing and retrieving the data in each activity using preference is a good method when large data is involved?”
My answer= No. Preference should be used for small primitive data (ie= The grade each student has in your class).
SharedPreferences stores the result in an XML file located within the App. If u want to find the XML 1) Run ur App. 2) go to DDMS 3) Using “File Explorer”, go into the directory Data-> data -> your package name (com.example.whatever) -> shared_prefs. There u should see an xml document.

Your storage options http://developer.android.com/guide/topics/data/data-storage.html:
Shared Preferences= Store private primitive data in key-value pairs.
Internal Storage= Store private data on the device memory.
External Storage= Store public data on the shared external storage.
SQLite Databases= Store structured data in a private database.
Network Connection= Store data on the web with your own network server.

I suggest u store your data in using SQLite. That way u can use Google’s massive datacenter. If you have your own server, then use Network Connection. External Storage would be my 3rd suggestion because external memory (SD cards) tend to have a lot more space than internal memory (Internal Memory= Internal Storage or Shared Preferences), especially on low-end phones. For example, I have a Samsung Gio. The internal memory is 181MB. The SD card holds almost 2 GB. BIG Difference!

If you’re doing all this because you have “multiple screens” you should be looking into fragments. Fragments is what allows u to control one Application on multiple screen sizes (ie= phone and tablets).

It sounds like u have it all backwards. The common Activity should have the power to control what Intents are called, not the other way around. You should have a common Activity that controls which Fragments show up based on the device. Then inside each individual Fragment Object, u can use SQLite to retrieve saved states.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文