调用谷歌玩游戏服务从一个片段 [英] Calling Google Play Game Services from a fragment

查看:157
本文介绍了调用谷歌玩游戏服务从一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一些谷歌玩游戏服务功能在我的Andr​​oid应用程序作为一个单独的活动,我现在正试图重写我的code作为一种(操作栏夏洛克)片段。我现在用的是提供GameHelper code在我的片段。

I have implemented some Google Play Game Services features in my Android app as a separate Activity and I am now trying to rewrite my code as an (Action Bar Sherlock) fragment. I am using the supplied GameHelper code in my fragment.

这是自动登录的正常工作。在用户启动的迹象失败,因为在GameHelper的StartResolutionForResult调用返回到活动的onActivityResult而不是片段。我已经验证这一切通过Log.D。我对此的理解是有限的 - 我应该怎么做才能解决这个问题?我曾尝试过不同的上下文,但StartResolutionForResult似乎只能接受一个活动作为它的背景。

An auto sign in works correctly. A user initiated sign in fails because the StartResolutionForResult call in GameHelper returns to the Activity's onActivityResult rather than to the fragment. I have verified all this by using Log.D . My understanding of this is limited - what should I do to fix this ? I have tried passing a different context but the StartResolutionForResult seems to only accept an Activity as its context.

推荐答案

在谷歌玩游戏服务API应该连接到一个活动的生命周期,而不是​​一个片段的生命周期。如果你的游戏逻辑是一个片段,可以实现onActivityResult的活动,并从那里打电话给你的片段。看看我们的键入数字挑战的样品,其中,除了是一个非常令人兴奋和上瘾的游戏< /嘲讽>中演示了如何处理片段。在键入每一屏幕的号是一个片段,并与他们需要的活动交流。

The Google Play game services API should be tied to an Activity's lifecycle, not a Fragment's lifecycle. If your game logic is in a Fragment, you can implement onActivityResult on the Activity and call your Fragment from there. Take a look at our Type A Number Challenge sample, which, apart from being a highly exciting and addictive game</sarcasm>, demonstrates how to deal with Fragments. Each screen in Type A Number is a fragment, and they communicate with the Activity as needed.

在这种特殊情况下,所有的游戏API互动的活动而成。然而,你也可以同样使活动手 GamesClient 对象的片段,以便能够实现其自身的逻辑。

In this particular case, all the interaction with the games API is made by the Activity. However, you could just as well make the Activity hand the GamesClient object to the Fragment so that it could implement its own logic.

在所有情况下,切记不要守在片段永久引用 GamesClient 的时间比你所需要的。这也可能是最好从活动查询它(通过一个接口,例如)当你需要它。这是prevent它从活动的生命周期中泄漏。

In all cases, remember not to keep a persistent reference to GamesClient in the Fragment for longer than you need. It's probably best to query it from the Activity (via an interface, for instance) whenever you need it. This is to prevent it from leaking during the Activity's lifecycle.

这篇关于调用谷歌玩游戏服务从一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆