开始一项带有大量额外意图的活动
我正在使用 Serializeable
extra 启动一项活动。这个额外的内容包含一个自定义对象的List
,其中包含一堆类型(主要是字符串)。我从项目的assets文件夹中读取数据,并使用GSON解析它(数据是JSON)。该文件大小约为 108KB。
在我的应用程序的生命周期中,所有数据都作为意图额外内容传递。这非常方便,因为我不必担心再次从资产文件夹重新加载数据,应用程序关闭恢复都已处理完毕,而且我不需要管理 SQLite 数据库(版本控制、查询等) )。
问题: 我发现传递这些额外内容可能会变得相当慢(使用所有数据启动一个活动可能需要 1.5 秒或更长时间)。我似乎也无法显示任何“正在加载”对话框,因为它似乎是启动活动并附加附加内容的阻塞调用。
问题: 我应该避免像我所描述的那样传递这些额外的东西吗?使用 SQLite 数据库与此数据交互的最佳选择是吗?您有什么建议可以避免使用 SQLite 数据库/全局静态变量来访问我的应用程序数据?
将我的 JSON 数据放入数据模型类并将它们作为意图附加传递是简单而美好的(看起来),而且我不想放弃它!
I am starting an activity with a Serializeable
extra. This extra contains a List
of a custom object holding a bunch of types, mostly Strings. I read in the data from the assets folder of my project, and parse it with GSON(the data is JSON). This file is ~108KB in size.
For the life of my application, all data is being passed around as intent extras. This is very convenient as i don't have to worry about re-loading the data from the assets folder again, app shutdown recovery is all taken care of, and i don't need to manage an SQLite database(versioning, queries, etc).
Problem:
I find that passing these extras around can become quite slow(starting an activity with ALL the data takes maybe 1.5 seconds or more). I can't seem to show any "loading" dialog either, as it seems to be a blocking call to start an activity and attach extras.
Question(s):
Should I avoid passing these extras around like I have described? Is the best option using an SQLite database to interact with this data? What suggestions do you have to avoid a lot of the fuss of SQLite databases/global static variables for accessing my application's data?
Slapping my JSON data into data model classes and passing them as intent extras is easy and nice(seemingly), and i don't want to give it up!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题并遵循了您已经描述的解决方案。我使用 SQLite DB 来存储信息,然后仅传递唯一的 id 作为额外意图,并在访问数据库时在被调用的活动上显示“正在加载...”进度对话框。
Prolly 使用 SQLite 是正确的选择。
I had the same problem and followed the solution you already depicted. I used an SQLite DB to store the information, then passed only unique ids as intent extra and displayed a "loading..." progress dialog on the called activity while I accessed the DB.
Prolly using SQLite is the way to go.
为什么不将数据加载到Application类,在Application类的onCreate中,然后从那里的每个活动访问?
Why don't you load the data to the Application class, onCreate of the Application class, and then access from every activity there?