16

I have activity A that instantiates GoogleApiClient, connects and starts processing in AsyncTask that may take seconds or minutes.

Meanwhile, user triggers activity B that instantiates it's own GoogleApiClient with a connection.

The question is: Can an app have multiple instances of GoogleApiClient connected and working simultaneously, or should I keep an app singleton with my own semaphores?

seanpj
  • 6,735
  • 2
  • 33
  • 54

3 Answers3

16

It's perfectly fine to keep as many GoogleApiClients as you want around, and there are often good reasons for doing so (separation of fragments, different accounts, etc.). It's also not particularly inefficient. The cost of two clients is less than 1% higher than the cost of one client.

It can be confusing if all of them are trying to resolve errors, so it's probably a good idea to make the Fragment clients all ignore connection failures, and have an Activity or Application level client responsible for resolving issues.

Hounshell
  • 5,321
  • 4
  • 34
  • 51
  • Is it bad to have a service with a googleApiClient connection always connected? I have an android wear watchface service that is always connected so it can receive messages from my watch. Do I have to keep googleApiClient connected all of the time to receive messages in the data layer? – DeNitE Appz Jan 23 '16 at 03:11
  • I don't know Android Wear, but I can answer the first part. It's not "bad", but it's certainly not "good". Holding a googleApiClient connection holds the service open. The service can be expensive to maintain, and the more parts of the API you use the more expensive it gets. This leads to increased load, which in turn leads to slower devices, user frustration, and the task manager killing your app. In the end, the general advice is hold the connection if the user is actively using your app, close it when you go into the background and poll periodically. Android wear might be different though. – Hounshell Jan 27 '16 at 18:09
  • 1
    @Hounshell if you are trying to receive messages directly in Activity/Fragment by using Wearable.MessageApi or Wearable.DataApi yes you will need to be connected to the GoogleApiClient. But, you can instead extend WearableListenerService and override onDataChanged() or onMessageReceived() so that you don't have to connect to GoogleApiClient at all by yourself. What you can do at this point is use broadcast receivers/event bus to transmit events to your Activity/Fragment. – Milan Feb 05 '16 at 12:56
6

Its possible to have multiple connected GoogleApiClients, just possibly inefficient. You do need to be careful using GoogleApiClient with AsyncTasks that it isn't disconnected if the activity goes away.

Consider managing the GoogleApliClient within a retained fragment. See http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
0

The issue is resolving by very common OOP Composition knowledge and Factory design pattern. Saying something about 1%, like @Hounshell below is not engineering approach.

Sergio Kosik
  • 192
  • 2
  • 6