I am trying to develop a simple chat application, only for study purpose.
My problem is that when the screen orientation is changing the TCP/IP connection is closed and recreated after orientation changes.
I know that Android destroys the activity and recreates it when changing orientation.
So my question: It is posible to change only view but everything else to remain intact?
Thanks
Well, the real issue here is that you are performing the TCP/IP connection on the main UI thread, which is a terrible idea because you risk blocking touch events and the layouts from being generated. A
NetworkOnMainThreadExceptionis thrown on most new devices these days to prevent this behavior too.What I would do is wrap the TCP/IP connection in a
Thread, and then have theThreadexecute in a worker (non-UI, invisible)FragmentwithsetRetainInstance(true). This protect yourThreadon configuration changes, as theFragmentwill remain in memory even when theActivityis destroyed. You can read more about this approach here.If the TCP/IP connection needs to exist across multiple
Activityinstances, you should use aServiceinstead.