Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having a problem with my game. If I press the "Home button" the game is paused... everythings fine, but if I then go back to the game all the resources are reloaded before I can continue the game. And it takes quite a bit. Is this normal, or is there a way to avoid the reloading?

I have write following code in onResume and onPause method. It loads same texture again and again on resume of game.

    @Override
    protected void onPause() {

            super.onPause();
            if (Utility.flagSound && mScene != null) {

                    if (mScene.getUserData().equals(Constants.GAME_SCENE))
                            Utility.isPlayLevelMusic = false;
                    else
                            Utility.isPlayLevelMusic = true;

                    audioManager.gameBgMusic.pause();
                    audioManager.levelBgMusic.pause();
            }

            if (this.mEngine != null && this.mEngine.isRunning()) {
                    this.mEngine.stop();
            }

    }

    @Override
    protected void onResume() {

            super.onResume();
            if (audioManager != null && Utility.flagSound && dataManager != null) {
                    if (Utility.flagSound) {
                            if (Utility.isPlayLevelMusic)
                                    audioManager.levelBgMusic.play();
                            else
                                    audioManager.gameBgMusic.play();
                    }
            }

            if (this.mEngine != null && !this.mEngine.isRunning()) {
                    this.mEngine.start();
            }

    }

I would be glad if anybody could help...

share|improve this question
    
Do you have to unload the texture? –  ClassicThunder Jun 25 '13 at 17:00
    
No, I already know how to unload texture. But my problem is that after pressing home button and again return to game cause whole game graphics get loaded. –  Siddharth Jun 25 '13 at 17:03
    
"cause whole game graphics get loaded" Is this bahavior forced or did you do this voluntarily? –  ClassicThunder Jun 25 '13 at 17:10
    
This behaviour is voluntary because on pause and resume I didn't write something like that. Also I post link that give same information. andengine.org/forums/gles2/… –  Siddharth Jun 25 '13 at 17:12
    
which library are you using? I don't see any reload call so do the engines unload the resources on pause()? How do your Resource objects look like? –  Quonux Jun 25 '13 at 18:03
add comment

2 Answers

This is normal behaviour. It happens because of the Android Activity Lifecycle destroying and recreating the OpenGL context onPause/onResume. From GLSurfaceView documentation:

Activity Life-cycle

A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients are required to call onPause() when the activity pauses and onResume() when the activity resumes. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

Because Android phones work with very limited resources, especially the older and low-end devices, there is not enough memory to hold multiple contexts with all the textures. The easiest way is to unload everything onPause and reload back onResume. This is partly handled by AndEngine and partly by Android.

If this is causing you issues, starting from Android 3.0, you can actually preserve the context. See setPreserveEGLContextOnPause for more information. Note that AndEngine might not count with this option and behave unexpectedly. Also note that as of now, about 30% devices still run Android 2.x where this is not available.

share|improve this answer
    
I know this type of behaviour of android activity life cycle. But when I look through other games in market and apply same test on it then those games didn't load graphics again. So it start as normally. But my game freeze for around 5 seconds to get its normal behaviour. Also in this link person get resolved this issue. andengine.org/forums/gles2/… –  Siddharth Jun 27 '13 at 2:40
    
5 seconds is a lot. How do you detect that it is one specific texture? This is more of a debugging task. –  sm4 Jun 27 '13 at 3:01
    
When I saw loading scene and load all texture of the game, at that time whatever process occur same type of process occur on game resume. Right now I perform following steps 1) start game, 2) press lock button, 3) press unlock button. So game resume method calls and whole texture loaded again. On resume and pause method I write above code. –  Siddharth Jun 27 '13 at 3:08
add comment

Try to modify method onSetContentView() in BaseGameActivity.java

with this code

protected void onSetContentView() {


    this.mRenderSurfaceView = new RenderSurfaceView(this);
    this.mRenderSurfaceView.setRenderer(this.mEngine, this);
    if(android.os.Build.VERSION.SDK_INT >= 11){
        this.mRenderSurfaceView.setPreserveEGLContextOnPause(true);
    }
    this.setContentView(this.mRenderSurfaceView, BaseGameFragmentActivity.createSurfaceViewLayoutParams());
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.