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.
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);
}