Question

I'm developing an App that, for example, It has 4 activities (A,B,C,D). You can navigate from A -> B -> C -> D

So, in the Activity A, I open a socket connection and I want keep it opened when I go to Activity B, then C, then back to B, then C or whatever.

I think that I can open the socket in Application context or a "Domain" context (with a Singleton) that my variale socket is "global", it isn't the problem.

The problem is that I want disconnet the socket when te user leave the app (Home button or Back button in Activity A)

How I can keep the socket opened always but disconnect when the user leave the app. I don't want to open the socket in every onCreate/onStart and close it in every onStop/onDestroy.

Thanks for your responses.

Was it helpful?

Solution

I think what you want is a bound Service. You can bind in onStart and unbind in onStop. Your Service can maintain the socket connection.

http://developer.android.com/guide/components/bound-services.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top