• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps socket object as static class field

manish411

Lurker
I am developing an application which incorporates socket programming. I need to refer to the socket object created in the starting application when connected to a server in any other activity in that application .
Can I define a class in an acitivity and define a socket as a static field in that class and refer to that socket object from any other activity in that application??
 
As long as a new instance of the Socket object is not needed with each new instance of the owning class, then yes, you can do that.
 
Instead of passing reference to the socket object among activities, another option is to put the socket object in your application, then in your activities you can use getApplication() to get the reference to the application then get the reference to the socket object. You might want to synchronize access to it in the application class as well.

Myself don't pass any object other than POJOs between activities. Hope this helps.
 
Back
Top Bottom