Rhisiart
Rhisiart

Reputation: 1086

Two fragments - use sqlite from both fragments or pass detail from one to the other?

Let's say I have an SQLite table that contains 10 columns of type text(20).

ListFragment would pull 4 columns from the database and display in the list using a SimpleCursorAdapter.

Upon selection, the ListFragment would pass the selected row_id to the DetailFragment where it would pull all 10 columns from the database to display (another query).

An alternative design would be to pull all data required up front in the ListFragment (but only display those that were required) and pass everything over to the DesignFragment through the intent or constructor. What I didn't like about this option was that the list would contain additional data that it would not need, while the original option would mean a secondary call to the database.

Which would be the preferred option?

Thanks.

Upvotes: 3

Views: 203

Answers (1)

Swayam
Swayam

Reputation: 16354

Well, I may not be an expert at this, but in my opinion, it is better not to query the database over and over again.

Query the data once (assuming that the contents of your database is not really HUGE) and pass them around using Intents.

However, instead of passing them via Intents, what I would personally advice you is to create an Utility class to contain all the details of the database and you can just initialize this class object once and then use the object where ever you need. Only thing you need to keep in mind is that you need to update this object details every time the content of your database gets updated.

(Eg. like the official Facebook Android SDK uses an Utility class to store all the user info, including access tokens and session ids)

Upvotes: 1

Related Questions