简体   繁体   English

为多个活动创建GoogleApiClient

[英]Creating GoogleApiClient for multiple activities

I am developing an android app with Google+ API. 我正在使用Google+ API开发一个Android应用。 I am having multiple activities which each require one instance of GoogleApiClient. 我正在进行多个活动,每个活动都需要一个GoogleApiClient实例。

As I understand from this post it is possible to call the same instance of GoogleApiClient for each activity. 当我从理解这篇文章 ,可以调用GoogleApiClient的同一个实例的每个活动。 My question is how do we create copies of the GoogleApiClient specifically? 我的问题是我们如何专门创建GoogleApiClient的副本?

Do we build one again with the .addApi(), .addscope() and implement onConnected method and OnConnectedFailedListener method again? 我们是否再次使用.addApi()、. addscope()构建一个,并再次实现onConnected方法和OnConnectedFailedListener方法? Because it seems repetitive and inefficient. 因为它似乎是重复的和低效的。 And wouldn't implementing these methods override the same methods from other activities too? 并且实现这些方法是否也将覆盖其他活动中的相同方法?

It's not expensive to create multiple instances of GoogleApiClient . 创建多个GoogleApiClient实例并不昂贵。 In fact it will help with efficiency if you use more than just one API. 实际上,如果您使用多个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. 因此,如果一项活动使用Plus,而另一项活动使用Drive,那么在进行Drive活动时,不必假脱机Plus服务。

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). 为了清楚GoogleApiClient ,建议您为创建的每个Activity,Fragment,Loader,Service或Application创建一个单独的GoogleApiClient实例(甚至我也忘记了其他一些实例)。

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. 如果您确实不想这样做,请使用应用程序上下文而不是活动或片段来创建GoogleApiClient并将其引用保存在Application对象中。

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. 为了解决这个问题,我使用了BaseGameUtil ...不确定是否使用它,但是如果您很简单,则可以让每个活动扩展BaseGameActivity,添加所需的方法,然后创建GoogleApiClient obj和getApiClient,然后让您可以在第二项活动中使用GoogleApiClient。

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. 如果您不使用BaseGameUtil,那么我认为您必须像上面所做的那样创建它,这很痛苦,至少basegameutil会为您做到这一点,而且您总是可以更改BGU中的内容,因为它们比库中的示例更多。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM