Henrik
Henrik

Reputation: 1995

How to handle activity life cycle involving sockets in Android?

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?

All input is welcome :-)

/ Henrik

Upvotes: 3

Views: 3119

Answers (1)

Yann Ramin
Yann Ramin

Reputation: 33177

Either prevent your activity from being restarted (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.

Upvotes: 6

Related Questions