I implemented a class called WebSocketClient
which provides the functionality to open and connect to a websocket, send data through the websocket, etc...
Now I want to use this class in my Activities. In order to be informed of incomming messages from the websocket i created a WebsocketListener
which can be registered in the WebSocketClient
so the activity which would like to communicate through the Websocket would implement this interface and register itself.
The problem is, how can I use the WebSocketClient
in multiple activities? My first idea was to implement the WebSocketClient
class as a Singleton
, so in each activity I am able to get the instance of the WebSocketClient via WebSocketClient.getInstance()
and register itself as a WebSocketListener
. Is this a good way of implementing what i want?
So if i am in Activity A
i would call WebSocketClient.getInstance().register(this)
, when i switch to the next Activity B
I also have to call WebSocketClient.getInstance().register(this)
which would change the current listener to the current active activity. This way i am able to use the WebSocketClient
in each activity.
Will this work? Does anybody have a better solution?
kind regards