I am working on an application in which I need a connection to a server. I also need to access this connection from different activities.
To achieve this I was going to override the Application
class and create the connection there. This would allow for easy interaction from every Activity as I could simply call getApplicationContext().getConnection()
to get access to my own connection class.
The problem with this approach is that the Application
class does not have any onDestroy()
method or similar in which I can release the connection and any related resources. I do not think that leaving it idle until onLowMemory()
is called is the best approach here.
I cannot add a custom release()
method, as I don't know when to call it (there are two Activities that can be the last one to be active, and depending on the users actions they do not know if the other is to be started when the active one is shut down).
Is there a good solution to this, should I just ignore releasing resources (before onLowMemory()
) or is there a better way to achieve what I want (possibly a Service
, but as there will be several calls to an underlying class it might get overly problematic with the Service?)