How to handle activity lifecycle using sockets in Android?

  • I have Android activity, which in turn triggers a thread.
  • In the stream, I open a persistent TCP socket connection.
  • When the socket connects to the server, dynamic data is loaded.
  • The thread sends messages using the Handler class to the action when the data has been received.

Now, if the user switches from portrait to landscape, the activity receives an onDestroy call. At this moment, I close the nest and stop the thread.

When Android switches to landscape mode, it calls the CreateCreate command again, and I need to reconnect the socket. In addition, all the data that needs to be received must be downloaded again, because the server does not have the ability to know what was sent earlier, that is, there is no “resume” function.

Thus, the problem is that there is a lot of data that constantly arise during changes in the landscape regime.

What are my options here?

  • Should I create a service that handles socket traffic in relation to the server, so I always get all the data that the server sent to the service.
  • Or do I need to disable landscape mode together?
  • Or it’s best to rewrite my server, which is a VERY BIG job :-)

All entrance is welcome :-)

/ Henrik

+3
1

( Android), , . , , .

+6

Source: https://habr.com/ru/post/1747772/


All Articles