Creating a GoogleApiClient for multiple actions

I am developing an Android application with the Google+ API. I have several actions, each of which requires one instance of GoogleApiClient.

As I understand from this post , you can call the same instance of GoogleApiClient for each action. My question is: how do we create copies of GoogleApiClient?

Will we build again with .addApi () ,. addscope () and implement the onConnected and OnConnectedFailedListener methods again? Because it seems repetitive and ineffective. And will these methods not redefine the same methods from other activities?

+8
java android google-plus google-api
source share
2 answers

You should not create multiple instances of GoogleApiClient . In fact, this will help with efficiency if you use more than one API. Only the requests you request will be redirected. Therefore, if one action uses Plus and the other uses Disk, the Plus service does not need to be buffered when you are in Drive activity.

To be clear, it is recommended that you create a separate instance of GoogleApiClient for each Activity, Fragment, Loader, Service, or Application object you created (perhaps even some others that I also forgot).

If you really do not want this, use the application context instead of the activity or fragment to create the GoogleApiClient and save the link to it in the Application object.

+12
source share

I had the same dilemma. To get around this, I used BaseGameUtil ... not sure if you use it, but if you do it later, it's simple, you can simply activate each activity of BaseGameActivity, add the necessary methods, and then create GoogleApiClient obj and getApiClient objects, which then give you the opportunity to use GoogleApiClient in the second action.

 mGoogleApiClient = getApiClient(); 

If you are not using BaseGameUtil, then I think you will need to create it, as you do above, that pain, at least basegameutil does it for you, plus you can always change something in BGU, since they are more examples than libraries .

Hope this helps.

+1
source share

All Articles