user2484014
user2484014

Reputation: 11

Android Is using Application Class as a contoller OK?

I would like to have all my Activities (7 to 10 different screens) submit data to a "controller" (sorry if I'm miss using the term).

Inside said controller data would either be uploaded or saved to data for uploading when no internet.
The controller would do checks and processing such as:

My goal is to have Activities do nothing more than collect the data and call the appropriate method (with a data object) asynchronously. The controller would know how to process the data for uploading or saving in the database.

Would placing these methods in an extension of Application be a bad idea?

It was mentioned that depending on application size this is feasable, but there could be better solutions.

Upvotes: 0

Views: 43

Answers (1)

dst
dst

Reputation: 3337

Depending on the size of your project, that would be a suitable idea. However, there are some other ways you should know before choosing the method you're actually implementing:

  • Using a ContentProvider for your data, an AccountAuthenticator and then sync to the server using an SyncAdapter. Advantages are a good abstraction, independence from activities and many built-in features (for example: Android executes your code without a big battery life impact). However, implementing all the stuff is quite much work at first. If you don't want to use the ContentProvider, the same technique works with a stub implementation as well, the same goes for the AccountAuthenticator.
  • Using a Service, probably IntentService, for your uploading needs. Advantage is that the Service has an independent lifecicle and thus is not directly related to your Activity; a Service can get restarted if it has been killed by the system. Still more work than just using some static methods.
  • Using a static method as you're proposing it (in your case, the Application object; not completely static, but compareable). Quite easy to implement, probably the best way if there are similar tasks in multiple activities; your AsyncTasks can send their result directly to the activitiy that started it. However not suitable for long-running tasks.
  • Implementing within the Activity; If code is only used once; listed for completeness only, not for your case. Basically the same as using a static method.

These are the ones that popped into my mind, there might be some others, too. Feel free to add/suggest additonal ones.

Upvotes: 1

Related Questions