4

I am using the new Google Play Game services to create a real-time multiple player game on Android devices. I have the communication between the games going, and basic mechanics. However, I have now realized that any time the game's activity is left, the room is disconnected. This concerns me for a couple of reasons:

  1. The game is likely to be played on a phone. An incoming call would definitely cause the phone activity to come up, disconnecting the room and hence the game
  2. The display could suspend, also disconnecting
  3. I had planned on having an ad displayed on the game screen. Clicking the ad would disconnect from the room/game

Is there a way to maintain a connection to the room, or possible reconnect to the room (the documentation definitely seems to preclude reconnecting) in order to allow interruptions such as those above while still allowing the player to continue with the game seamlessly?

Thanks, Rob

  • Additional information- I am using the BaseGameActivity example class that disconnects on the activity's onStop() event. Would it be here to make some changes- such as releasing the resources on another of the activity's lifecycle events, like say onDestroy? – user2252106 Jul 04 '13 at 12:18
  • You might probably need to use a service to prevent room disconnect. – Aladin Q Jul 05 '13 at 10:53
  • Is there documentation on using a service to maintain a room's connection? This other [answer](http://stackoverflow.com/a/16845784/2252106) seems to pretty strongly suggest the client is connected to activities? – user2252106 Jul 10 '13 at 14:30

1 Answers1

1

I had this same issue, though for me it was when a user rotated their device (the activity would be destroyed, and along with it the connection to Google Play Services). I think my solution should work for you too. I answered my own question here:

In a nutshell, here's the verbal explanation (see link for code samples):

A device orientation change will destroy the MainActivity extends BaseGameActivity, and with it your game state (ie. your connection to Google Play Services). However, we can put all our GameHelper code into a 'headless' Fragment (a fragment without a UI), with setRetainInstance(true) declared. Now, when our MainActivity extends FragmentActivity is destroyed on an orientation change, the headless fragment gets stopped, and even detached, but not destroyed! (onDestroy() is not called) When MainActivity is re-created by Android, our headless fragment gets re-attached to it automatically. At this time, in our headless fragment, onCreate() is NOT called. So onCreate() is the place we connect to GameHelper. We can disconnect from GameHelper in onDestroy() because this will never get called, except when the Application finishes (which, at that time, it's ok to kill our connection).

Community
  • 1
  • 1
briggsm
  • 455
  • 3
  • 15