Creating GoogleApiClient for multiple activities
Asked Answered
M

2

9

I am developing an android app with Google+ API. I am having multiple activities which each require one instance of GoogleApiClient.

As I understand from this post it is possible to call the same instance of GoogleApiClient for each activity. My question is how do we create copies of the GoogleApiClient specifically?

Do we build one again with the .addApi(), .addscope() and implement onConnected method and OnConnectedFailedListener method again? Because it seems repetitive and inefficient. And wouldn't implementing these methods override the same methods from other activities too?

Margo answered 27/6, 2014 at 15:11 Comment(0)
J
14

It's not expensive to create multiple instances of GoogleApiClient. In fact it will help with efficiency if you use more than just one API. Only the services you specifically request will be spooled up. So if one activity uses Plus and another uses Drive, the Plus service doesn't have to be spooled up when you're on the Drive activity.

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

If you really don't want to do that, use the application context instead of an activity or fragment to create the GoogleApiClient and hold a reference to it in an Application object.

Jujitsu answered 7/8, 2014 at 19:19 Comment(3)
I'm considering keeping my reference to GoogleApiClient in a static and funnel work to it in a message queue from several different places in my app (i.e.: from an Activity or from Services as a result of receiving a push message). Can you think of any red flags in keeping a static reference to GoogleApiClient, using the Application context? It seems little different from keeping it in the Application object.Gangrene
Using application context comes with implications. The service can be expensive to maintain. Keeping this up when you don't need it will cause a significant amount of extra CPU and memory use when can run you into problems on low-end devices, and is more likely to cause your app to be closed when in the background. Keeping a reference to it tied to the application context is frowned upon for these reasons. If you're going to do that anyway, it doesn't matter if you store it in the Application object or some other singleton.Jujitsu
Thanks for the quick response! I realized that I can use blockingConnect on GoogleApiClient and since I'm doing all my work in a background thread I figure I can just use blockingConnect on GoogleApiClient, register for my geofences (assuming a successful result on blockingResult) and disconnect right after. I feel better about doing it this way rather than keeping the client and context around forever.Gangrene
C
1

I have just had this same dilema. To get round this I used the BaseGameUtil... not sure if your using that but if you are then it is simple you can just have each activity extend the BaseGameActivity, add the required methods and then create a GoogleApiClient obj and getApiClient which will then give you the means to use the GoogleApiClient in your second activity.

mGoogleApiClient = getApiClient();

If your not using the BaseGameUtil then i think you would have to create it like you do above which is a pain, at least the basegameutil does it for you, plus you can always change whats in the BGU as they are more examples than libraries.

Hope this helps.

Clearwing answered 30/7, 2014 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.