0

This is the situation: my app has one activity, handles a long-lasting TCP connection and updates the UI dynamically with a lot of stuff (HashTables of objects, buttons, ArrayLists, adapters...). At the moment, I handle the connection with an AsyncTask, and looking for a way to save:

  • the connection
  • all the variables, like dynamically created views

when the screen rotates or the user receives a call, I found this: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html which looks pretty cool to me. But I also read everywhere that I should use a service instead, because it's better for long lasting TCP connections. Why AsyncTask:

  • it looks like I can save the TCP connection using a Fragment
  • I'll have to do something similar anyway, in order to save objects, buttons, etc from rotation/calls
  • I have only one activity that needs the connection (aren't services indicated for more than one?)

Why service:

  • no need to use Fragments to save the TCP connection (is it true? what kind of service, normal, IntentService since I only have one client, or bounded, should I use to do this?)
  • better for long-lasting connections

When I say "service", I assume that I'll have to run an AsyncTask inside the service anyway, so it doesn't handle the connection on the main activity. What should I do?

user2502368
  • 467
  • 1
  • 4
  • 16

1 Answers1

0

In general, use an AsyncTask when the lifecycle of the background thread is contained within the lifecycle of a single activity. Otherwise, use a Service.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82