Keeping the connection in the room

I use the new Google Play Game services to create a real-time multiplayer game on Android devices. I have a connection between games and basic mechanics. However, I now realized that at any time when the game activity remains, the room is turned off. This concerns me for several reasons:

  • The game will most likely play on the phone. An incoming call will definitely lead to the appearance of phone activity, turning off the room and, therefore, the game.
  • The display can pause as well as turn off
  • I planned to show an ad on the screen of the game. Clicking on an ad will disconnect from the room / game

Is there a way to maintain a connection to the room, or possibly reconnecting to the room (the documentation definitely prevents reconnecting) to allow interruptions like the one above while still allowing the player to continue to play unhindered

Thanks Rob

+4
source share
1 answer

I had the same problem, although for me it was when the user turned his device (the action will be destroyed, and with it the connection to the Google Play services). I think my decision should work for you too. I answered my question here :

In short, here is a verbal explanation (see link to code samples):

A change in device orientation will destroy MainActivity extends BaseGameActivity , and with it your game state (i.e. your connection to Google Play services). However, we can put all of our GameHelper code in a "headless" fragment (a fragment without a user interface), with declared setRetainInstance(true) . Now that our MainActivity extends FragmentActivity destroyed when the orientation changes, the headless fragment stops and even disconnects, but is not destroyed ! ( onDestroy() not called) When MainActivity recreated by Android, our headless fragment is automatically attached to it. At this time, onCreate() NOT called in our headless snippet. So, onCreate() is the place we connect to GameHelper. We can disconnect from GameHelper in onDestroy() , because it will never be called, except when the Application ends (which at that time it was normal to kill our connection).

+1
source

Source: https://habr.com/ru/post/1489698/


All Articles