6

In my game app I would like the user to sign in to Google Play Services during the starting activity so I had it subclass BaseGameActivity. Then a separate activity is started for the game, at the end of which I want to update a leaderboard using Google Play Services, which requires calling BaseGameActivity.getApiClient().

How should I use Google Play services from a different activity than the one that subclasses BaseGameActivity?

Two options I thought of were: pass a reference to the starting activity, or use a handler and send a message to the starting activity. But I don't know which would method would be better to use (or if a third way is better) and it seems like this might be a fairly common situation.

GDanger
  • 1,641
  • 2
  • 22
  • 35

1 Answers1

4

If you want to use GameHelper with multiple activities it is best to implement it without using BaseGameActivity and then pass your GameHelper instance between activities (or keep a static instance somewhere).

You can find instructions for using GameHelper directly on this page (see the Using GameHelper without BaseGameActivity heading).

Just make sure that you place the required GameHelper calls (especially onActivityResult) in all of your activities that make use of it. As for sign-on/sign-out you will need to determine for yourself in which activity(s) to place those, depending on the flow of your app.

free3dom
  • 18,729
  • 7
  • 52
  • 51
  • Is there any reason to not subclass both activities from `BaseGameActivity` and in the one that I don't want to automatically sign-on put `getGameHelper().setMaxAutoSignInAttempts(0);`? – GDanger Jun 12 '14 at 19:23
  • You could do that but the `onCreate()` of the `BaseGameActivity` would recreate the `mHelper` for each activity and the state of the new object would not necessarily be correct - i.e. the new object's internal states might not reflect whether you are already signed-on (or vice-versa). I'm sure there is a way to get it to sync up properly, but it seems like alot of effort as opposed to just using the `GameHelper` independently. – free3dom Jun 12 '14 at 21:52
  • 2
    The issue with using `GameHelper` independently is AFAIK there is no good way to pass unserializable/parcelable objects (such as `GameHelper`) between activities and making it static seems like a bad idea as `GameHelper` holds a reference to the activity that created it. – GDanger Jun 12 '14 at 22:00
  • 2
    You are absolutely correct, I hadn't even considered that it keeps the original activity as a reference. You could replace those values, but that seems like a very bad way to go about it. I suppose they designed the `BaseGameUtils` with single activity applications in mind (which most games would be anyways). It seems that implementing it in each activity is the only **proper** way to do it, but it will need to disconnect/reconnect for each activity - which should not be a problem as long as it doesn't happen too frequently. – free3dom Jun 12 '14 at 23:52
  • It appears that the best practice is to have one activity that has `GameHelper` tied to it and use fragments if at all possible. – GDanger Jun 13 '14 at 06:12
  • 4
    I just saw [this](http://stackoverflow.com/a/24235549/1326821) answer, which seems to be good way for using Google Play services with multiple activities. – free3dom Jun 17 '14 at 08:05