views:

268

answers:

1

Hello all,

  • I have an Android activity which in turn starts a thread.
  • In the thread I open a persistent TCP socket connection.
  • When the socket connects to the server dynamic data is downloaded.
  • The thread sends messages using Handler-class to the activity when data has been received.

Now if the user happens to switch from portrait to landscape mode the activity gets an onDestroy call. At this moment I close the socket and stop the thread.

When Android has switched landscape mode it calls onCreate yet again and I have to do a socket re-connect. Also, all of the data the activity received needs to be downloaded once more because the server does not have the ability to know what has been sent before, i.e. there is no "resume" feature.

Thus the problem is that there is alot of data which is resent all the time when landscape mode is changed.

What are my options here?

  • Should I create a service which handles the socket traffic towards the server thus I always got all the data which the server has sent in the service.
  • Or should I disable landscape mode all together perhaps?
  • Or would my best bet be to rewrite my server which is a VERY BIG job :-)

All input is welcome :-)

/ Henrik

+3  A: 

Either prevent your activity from being restarted (http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android) or implement a Service which your application manages. The Service method is very popular with applications I have seen, and allows you some flexibility when tearing down the connection.

Yann Ramin
Definitely create a `Service`; you'll have the same "lifecycle" problems not only if the screen rotates, but if the user presses Home, a call comes in, the screen goes idle etc...
Christopher
Thanks for the input! Really appreciate it.
Henrik