Interrupt handling
The game loop is a continuous process. Whenever an interrupt occurs, it is necessary to pause every running thread and save the current state of the game to ensure that it resumes properly.
In Android, any interrupt triggers from onPause()
:
@Override protected void onPause() { super.onPause(); // pause and save game loop here } // When control is given back to application, then onResume() is // called. @Override protected void onResume() { super.onResume(); //resume the game loop here }
Now, we need to change the class where the actual game loop is running.
First, declare a Boolean to indicate whether the game is paused or not. Then, put a check in the game loop. After that, create a static method to deal with this variable:
private static boolean gamePaused = false; @Override public void onDraw(Canvas canvas) { if(gameRunning && ! gamePaused) { MainGameUpdate(); RenderFrame(canvas); invalidate(); } else if(! gamePaused) { /...