1

I'm developing an app that uses an internal sqlite database. The MainActivity contains a Fragment.This fragment has a ViewPager which contains other 3 fragments, each of these 3 fragments has a ListView in it. when you click an item of the list, a second Activity is opened containing another ViewPager that has fragments showing the complete details(a bitmap and Strings) of each list item.

My question is, what would be better for performance ?

  1. To make constant calls to the database each time the user scrolls through the detail fragments(ViewPager).

  2. Make only three calls to the database (one for each listview in the mainActivity), to create 3 ArrayList of objects and keep them in memory. The list could have lots of items.

  3. Is there something else I'm not considering ?

3
  • If you have a few records, load them in arrays. If you have tons of records, load them on demand. Commented Mar 4, 2014 at 1:06
  • 1
    a) sounds like a terrible idea, individual queries are slower than 1 query that results in a list b) sounds reasonable (although you should not blindly do 3 queries upfront, do them when they are required) I'd prefer c) using a CursorLoader (github.com/commonsguy/cwac-loaderex if you don't use a ContentProvider) since they cache results in memory, load on demand, execute queries async, .. Commented Mar 4, 2014 at 1:41
  • @zapl With the use of Loaders, and their ability to retain their data, now on my mind. I will get rid of the second Activity, and do everything in the MainActivity, so i can use its LoaderManager to share the Loader instance's data across all the fragments. right? Let's say i create a Loader instance to fill a list when my app starts, the same loader that the rest of fragments will use later on. Apart from the benefits of using Loaders (asynchronous, monitor the data source...). How much difference there is between a Loader retaining the data and keeping an ArrayList of objects in memory ? Commented Mar 4, 2014 at 22:17

1 Answer 1

1

3. Is there something else I'm not considering ?

Yes, use ViewPager.setOffscreenPageLimit instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.