0

I have a leaderboard set up in my Android game and it seems to work pretty well.

From the main menu the player can press the 'show scores' button to simply view the leaderboard and when the player's game is over (either through losing all their lives or completing the game), they are given the option of submitting their score. If this is pressed, the score is submitted and the leaderboard displayed to show the new score.

I have my app set up so that it will return to it's current scene (menu, game etc.) if it is temporarily interrupted.

However, I can't work out how to 'know' if the leaderboard is currently being shown to the user. (Well, I do know how to set this, rather I don't know how to 'unset' it when the leaderboard is exited)

I have a variable set up which determines which action my app should take when 'onSignedInSucceded()' is called like so:

@Override
public void onSignInSucceeded() {

    //If the flag is set, then display the leaderboard
    if (signInAction==SHOW_LEADERBOARD){
        displayLeaderBoard();
    }

    //If the flag is set, then display the submit score
    else if (signInAction==SUBMITSCORE){
        submitScore();
    }

    //Otherwise, reset the flag and take no action
    else {signInAction=NO_ACTION;}
}

However, what happens is that, lets say the user pressed the 'show scores' button, we set the signInAction to SHOW_LEADERBOARD and then connect. The leaderboard is displayed.

The user then presses the back key and returns to the app. If the app is now interrupted, onSignInSucceded() is called and the leaderboard is displayed again. (but it wasn't being displayed when the app was interrupted therefore this diminishes the user experience).

It would be great it, when the user presses the 'back' key to exit from the leaderboard, I could set the signInAction back to 'No_Action' - how can I do this?

Just for additional information, my app is a single activity and utilises a custom scene manager so in my Activity class, I can do things on back press like:

If (CurrentScene = mainMenu){
    //Do Something Here
}

However, the leaderboard is not a custom scene. So I'm not sure how to go about this.

I guess this question could probably best be summed up as 'How can I detect a back key press from Google Play Leaderboard? (when the user exits it back into the app)' - any help would be appreciated.

Zippy
  • 3,826
  • 5
  • 43
  • 96
  • Why don't you just use `onPause()` and `onResume()`? That's exactly what you're trying to implement, it sounds like, unless I'm misunderstanding? – Kevin Coppock Jun 13 '14 at 00:27
  • Hi @kcoppock, could you elaborate? When my app is run / resumed, if the user is signed into Play Services, onSignInSuceeded() will be called, and if signInAction is still set to SHOW_LEADERBOARD, then it will trigger DisplayLeaderBoard(); - So I need to say 'User has exited leaderboard, so set signInAction to NO_ACTION' so this doesn't happen - hope this makes sense :-) – Zippy Jun 13 '14 at 00:41
  • I might be misunderstanding the flow of your app. However, from saying you need to know whether or not the leaderboard is showing, it seems like whenever you set that flag (e.g. signInAction = SHOW_LEADERBOARD), you can always clear it back to NO_ACTION in `onResume()` in your Activity. Launching the Leaderboard will pause your Activity. You can use that information to say that whenever your Activity is resumed (`onResume()` is called), you're obviously no longer in that state. – Kevin Coppock Jun 13 '14 at 00:44
  • So, user clicks "Show Leaderboard", you set signInAction to SHOW_LEADERBOARD. onPause() will be invoked. In onResume(), just set signInAction to NO_ACTION. – Kevin Coppock Jun 13 '14 at 00:44
  • Yeah, but this will mean the leaderboard is never show on re-start, what I need is that the leaderboard will be displayed on resuming if it was being shown when the app was paused. And not to be displayed if it wasn't being displayed when the app was paused :-) – Zippy Jun 13 '14 at 00:55
  • Hmm, okay, I think I'm probably misunderstanding. I don't really have any experiences with the games library. One suggestion is, use `startActivityForResult()` to show the leaderboards, and listen in `onActivityResult()` for the request code that you give to identify the leaderboard request. That would mean for certain that they've returned from the leaderboards. – Kevin Coppock Jun 13 '14 at 00:58

1 Answers1

1

You should be able to use onActivityResult() to clear signInAction. You'll need to check the request code and maybe the result codes depending on your application.

Some of the possible results will be:

  • Activity.RESULT_CANCELED (this is the one you're interested in)

  • GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED

but you'll probably be best off handling them all the same way.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    //just returned from displaying the leaderboard
    if(requestCode==REQUEST_LEADERBOARD)
        {
        signInAction=NO_ACTION;
        return;
        }

    super.onActivityResult(requestCode, resultCode, data);
    }
Brian DeWolff
  • 326
  • 1
  • 5
  • Nice answer @BrianDeWolff - thanks. I've implemented it and it works great, I can actually simplify it as I only every start one activity I don't really need to check the requestCode, but I did, just to be on the safe side - thanks!! – Zippy Jun 24 '14 at 11:33