Reloading Resources on Resume

| | August 18, 2015

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…

2 Responses to “Reloading Resources on Resume”

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

Leave a Reply