views:

129

answers:

1

Hello,

I'm starting an Android app which will have multiple Activities. It will be using a REST api with OAuth. I have tested the OAuth and have successfully parsed the returned xml.

My question has to do with the architecture of designing a client like this. I have a HttpClient and OAuthConsumer objects which will be used from the various Activities. I started designing a DaoFactory pattern which would provide DAOs for the various API calls. But I don't know how to make this pattern work in the Android framework. Each Activity can come and go and the user moves through the screens. Where should I store these objects? Should I make them members of the Application class and access them with getApplication()? Should I put the network calls and xml parsing code in a Service for each of the Activities to use?

I guess I'm trying to make this like a 3 tier webapp, but was looking for examples of other rich Android clients. I appreciate any help or advice.

+1  A: 

It depends on the details of your app of course. If the network calls can continue in the background, independently from any visible activity, then you probably want to put them in a Service, and have each interested Activity bind to the service.

BTW applying patterns commonly used in enterprise Java to Android is not always a good idea: see Designing for Performance.

Mirko Nasato
Yea I guess I'm just trying to wrap my head around the idea of having a "federation of activities". When I started with a single Activity, it was easy because it had a member field for the OAuthConsumer. As I start adding more Activities, I realize he needs to move somewhere else, just not sure where. Easiest solution seems to be the Application class. Each activity could grab the OAuthConsumer and HttpClient instances from the parent Application instance. But I read somewhere that the Android developers really don't encourage this approach. Not sure what the recommended approach is.
GrkEngineer
So I went down the path of using a Service, but I really think for what I need, I just need to subclass the Application class. I just need global variables which can be shared across my Activities. There were too many gotchas like not being able to make calls to the service in the onResume function. Even though I was trying to bind to the service in onCreate, it was only getting created after the Activity's onResume.
GrkEngineer
Well if you just need to share some data between activities then using Application is simpler. I was assuming you needed to perform network calls even when no Activity was visible.
Mirko Nasato