Search code examples
androidandroid-activitygoogle-play-servicesgoogle-play-gamesleaderboard

Main Activity pausing for a few seconds when returning from Leaderboard (due to reloading of textures)


My App has a single activity.

When I launch my Google-Play-leaderboard (which is a separate activity) and then return to my app's main activity, the whole thing pauses (sometimes for up to 4 or 5 seconds) before normal service is resumed.

I've searched high and low for the past 5 hours, but I just can't work out why this is happening.

Within my app's activity, I use a custom scene manager so the use can navigate from one 'scene' to the next (somewhat 'simulating' separate activities) when returning to my main menu from any of my other scenes, everything is instantaneous. I only get this issue when returning from an outside activity (my play services leaderboard).

Here is my leaderboard code:

Would appreciate any pointers anyone could offer.

if (scoresButtonPresses){

    displayLeaderBoard(); 

}

void displayLeaderBoard(){

    //Display the leaderboard if already signed in
    if (checkSignedIn()){               

        startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), leaderboardID), 1);

    }

    //if not already connected, then set flag and connect to play services before displaying leaderbaord
    else{
        signInAction=SHOW_LEADERBOARD;                      
        getGameHelper().beginUserInitiatedSignIn();
    }   


}

@Override
public void onSignInSucceeded() {

    //If the flag is set, then display the leaderboard
    if (signInAction==DISPLAY_LEADERBOARD){
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), leaderboardID), 1);
    }

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

Edit

I think I've worked out what the issue is but I need to know how to stop it happening.

So, in my onSurfaceCreated() method, I'm loading all of my bitmaps and setting the textures like so:

    atlas= BitmapFactory.decodeResource(view.getResources(), R.drawable.mainatlas, BMFOptions);

I then set it as a texture like so (setTexture is a method in my custom Quad class:

 sprite1.setTexture(view, atlas);

I then recycle the image as it's no longer required....

atlas1.recycle();

Therefore, what's happening is that when I return from my leaderboard activity to my main game activity, it calls onSurfaceCreated() again and the pause is because it's re-loading all the graphics / textures. Unfortunately, this is a requirement because this is an openGL app and it's recommended to load textures here incase the GL context is lost.

What I'm not sure about is, is my GL Context really being lost? Just by firing up a leaderboard activity and immediately returning to it's parent activity? If it's not, is there any way I can check to see if it's been lost and if it hasn't then skip all the loading?

I read somewhere that if onSurfaceCreated gets called it means either and orientation has changed (which it hasn't as my app is locked to landscape), or the GLContext has been lost. Is this correct? If so how can I deal with this pause while all the textures are re-loaded?!


Solution

  • Enable the debug log for the GameHelper:

    getGameHelper().enableDebugLog(true);
    

    That'll give you a lot more information on what the Google services are doing, then check the LogCat for the time gap, which should indicate where it's hanging up.

    The only methods being called when coming back from the leaderboard should be onResume() and onActivityResult(). It could be helpful to have those methods posted here, as well as the debug LogCat.

    Zippy: Hi @BrianDeWolff, thanks. I think I've found out what the problem is, as I suspected the fact that it's a leaderboard activity has nothing to do with the problem, please see my edit in my question. It's because of textures being (re)loaded in my onSurfaceChanged() method. Would be grateful if you have any suggestions. Thanks! :-)

    BrianDeWolff: That makes sense. I don't have experience working with GL but this seems to be a pretty common complaint/question. For example: https://gamedev.stackexchange.com/questions/12629/workaround-to-losing-the-opengl-context-when-android-pauses