Multiple instances of Google API Client?

seanpj picture seanpj · Mar 31, 2014 · Viewed 8.2k times · Source

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?

Answer

Hounshell picture Hounshell · Apr 18, 2014

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.