0

I am currently working on an messaging application for Android that communicates with other devices using Nearby Messages API. Since this is the first time that I work with this API, I would like to know if there is a pattern or strategy to handle the connections.

For instance, when the user changes the activity (e.g. opens a new conversation), I would like to keep the connection active, so I would like to work with a Connection Manager or something to keep listening and parsing the messages.

horro
  • 1,262
  • 3
  • 20
  • 37

1 Answers1

1

We kept working on our code, and finally we decided to implement a ConnectionManager as a single instance. This way all the activities in the application are able to access to the same methods. We also avoid to have several instances of GoogleApiClient, and then know if we are connected or not (e.g. isConnected() method).

However, we also needed in some methods the context or the activity, but we solved passing these parameters as arguments in those methods.

To sum up:

  • Singleton pattern: avoid creating several instances of the same GoogleApiClient
  • Proxy pattern: encapsulate GoogleApiClient methods in a class that handles the whole connection, instead of delegating this task on activities
horro
  • 1,262
  • 3
  • 20
  • 37