3

I'm starting to learn Android and i would like know the best practice to organize my app. I'm used to decompose my project in 3parts; Models, Views and Controllers... so in a first case I want to create my package sources like this:

- project.models.* // My all models
- project.ui.*     // My all activities

My app needs to communicate with a webservices so:

- project.models.* // My all models
- project.ui.*     // My all activities extends ListActivity etc...
- project.io.*     // Interfaces between models and webservices.

But I have read a lot of examples and it does not seem to be the right method... I don't know if it is a good way to try to create a MVC with Android.

How can I start my app in the best way?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
sahid
  • 2,570
  • 19
  • 25

4 Answers4

8

MVC is already implemented.

See here

Community
  • 1
  • 1
javisrk
  • 582
  • 4
  • 19
1

The problem of using mvc is that it's hard test. I would recommend you using MVP instead.

For the web service you can try learning syncAdapters. Here's a nice blog post of how to use them: http://naked-code.blogspot.com/2011/05/revenge-of-syncadapter-synchronizing.html

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • 1
    I'm a guy trying to learn all this stuff and some of it just goes over my head. So... I'm curious to know why you say mvc is hard to test. I thought that of the advantages of decomposing the UI using MVC was testability - e.g.: If your view is dumb, you can test the controller and model right? Are you saying that view logic like disabling and enabling controls is hard to test? If so, wouldn't MVVM be a better pattern than MVP in this case? Thanks! – dalcantara Feb 11 '12 at 13:32
  • @Buzzer: It's just my opinion. Give it a try! – Macarse Feb 11 '12 at 18:53
1

In terms of the "best way to organize an app which communicates with webservices" there's a session from GoggleIO 2010 that I found very useful:

http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

It's a high level description of how the Twitter application for Android decomposes responsibility for storing data and communicating with the server. With the pointers from the session I was able to dig up some great info about IntentServices, using ResultReceivers to get notifications back to the UI thread, and using ContentProvider data updates to drive refreshes.

Once you've got those parts stitched together to provide the base it's much easier to assemble a clean MVC on top of it.

mikerowehl
  • 2,222
  • 1
  • 17
  • 20
0

Keep in mind to decouple your UI from webservice calling. In ideal case, all webservice communications are in separate class / thread. UI shall be decoupled from it.

UI can either poke your service processing class (controller) for new data, or controller can push them actively via intents. You may also think about implementing service

In my casual games, I use periodic alarm to synchronise local highscore cache with server (every 30 minutes), and notify UI via intent when top score is changed.

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35