1

Suppose I have three activities A,B, and C. These activities all use a socket connection to receive data. The socket connection is a Telnet connection, so it can't be closed and reconnected arbitrarily.

If the current back stack is A > B > C, Activity A may be destroyed by Android, so I can't close the socket in onDestroy() method, otherwise Activity C would not be able to receive data from the socket.

I want to close the socket when the app is terminated. I can manually close it when I press the BACK on the root activity. However, if the app is killed when it is on the background, how do I handle this situation to close the socket and release related resource?

Thank you!!

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Mong
  • 21
  • 1
  • 2

1 Answers1

0

Actually I would recommend you use only one Activity/task to communicate. All others should use the CommunicationManager. Also remember that onDestroy does not always get called. Use onPause on your start/end - for Activities - to be sure that you end all your stuff in an well organized and secure way.

Basically I would create an asynchronous background task that handles the communication so your UI does not get blocked.

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
user387184
  • 10,953
  • 12
  • 77
  • 147