开发者

Passing arrays between activities or take data from database

I have one question about android basic concepts.

I have an Activity that pass two arrays of Integers to another Activity via an Intent. One array is an array of IDs (Integers) and the other is an array of Names (String). I have all this information also in an SQLite Database.

So my question is: What is better, passing all the info using the intent or passing only the ID's array and take the Names directly from the database wh开发者_开发技巧en the OnCreate of the second activity is invoke?.

Thanks.


The best approach is to send as little info through intents as possible. This will keep your code for storing / restoring data in the proper places, as well as reduce the transition time between activities.

Since you've already got that information stored in the DB, and already presumably have the methods to store and retrieve that information, why add more work for yourself writing code to bundle it all up for an intent? Just send the identifying info and let the rest of your framework do the work.


In general, it's preferable to retrieve large amount of data from DB than pass it to intent since parceling into intent requires memory. So if you want to send, for example, an array of complex objects which contain bitmaps, the OutOfMemoryException can be thrown, which won't be thrown in case you retrieve those objects from DB or another persistent storage directly.

But in your case it seems that the difference won't be noticed, as you pass only strings. So you can go with passing your data via intent and save resources for opening/closing DB.

This isn't the postulate however, so any comments would be appreciated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜