1

Let's say I have a simple app consisting of:

A Fragment with a list view to display a list of books

An Activity to hold the fragment

Some web component that will issue a query to a server somewhere to get the list of books to display

What's the best way of wiring these components together? I see a couple of options but which is best (are there any best practices around this)?

  1. Activity issues query to webserver (via AsynTask / new Thread), and pushes results to fragment

  2. Create another fragment without a UI and have this issue the query via AsyncTask / new Thread (like option 1 but will remove the activity being destroyed on screen rotation issues)

  3. Fragment directly calls the webserver, activity does nothing

  4. Add an intent service to do the query and then broadcast results to either:

    a) Activity - which pushes them to Fragment

    b) Fragment receives broadcasts directly

Are any of these considered a better approach than the other?

Various Fragment related posts have said "consider the fragment a view and the activity the controller" so this would suggest option 3 was maybe not such a good idea.

Option 2 seems a hybrid of considering the activity a controller, kind of delegating some of the 'controller' functionality to another fragment but crucially not the 'view' fragment

Option 4 seems interesting but then you have the issue of having to broadcast all the results (whereas 1 and 2 you will have the actual objects). Granted you could make the broadcasts more efficient (and secure) by using the LocalBroadcastManager but you're still having to broadcast all your web results.

What if I was downloading images (something substantial) rather than (for example) JSON messages that are turned into lightweight Parcelable objects?

As I said, I can think of many ways of doing this - what do we think is the right way?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1736191
  • 313
  • 1
  • 3
  • 7
  • 1
    I'm done with `Fragment`s. I'm using the `Activity` as a controller, and create a custom `ViewGroup` class which is the parent of all shown `View`s. `IntentService`s are a nice way to handle asynchronous network requests. – nhaarman Aug 06 '14 at 19:17
  • I feel 4th option is best:Add an intent service to do the query and then broadcast results to either: a) Activity - which pushes them to Fragment b) Fragment receives broadcasts directly – rupesh jain Aug 06 '14 at 19:17
  • 1
    Also, don't use the BroadcastManager to broadcast the results, use it to notify that there *are* new results. Serializing data into intents is costly. – nhaarman Aug 06 '14 at 19:28
  • That last comment is interesting, if I was to broadcast to notify that there _are_ new results, how would I then transfer the results to the activity? Via a ContentProvider? Isn't that overkill for data that isn't intended to leave your application? (If I remember rightly the opening part of the ContentProvider docs say something along these lines). Or are you saying replace the IntentService and instead bind a service to the activity? – user1736191 Aug 06 '14 at 19:43
  • 1
    I'm just using a dedicated data manager class, which could be reachable through the `Application` instance. – nhaarman Aug 06 '14 at 19:50
  • You are most wise :) Thanks! – user1736191 Aug 06 '14 at 19:56

1 Answers1

1

All your problems can be addressed with Loaders. It's what they were designed for. Check out the dev guide: http://developer.android.com/guide/components/loaders.html

Simon
  • 10,932
  • 50
  • 49