Domanda

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?

È stato utile?

Soluzione 2

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

Altri suggerimenti

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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top