Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

android - Calling Google Play Game Services from a fragment

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...