1

I am currently creating a game which has a MainActivity as well as a SurfaceView. The SurfaceView is not it's own activity, but is simply called in the MainActivity.

What I am trying to do is have a button on the MainActivity (Pause / Resume) which will pause rendering on the SurfaceView and resume it again when clicked.

I am not entirely sure how I would do this, inside of my SurfaceView (GameView) I can stop my GameLoopThread but if I try to simply restart it (pause and then resume the game) it does nothing.

I have tried with pausing/resuming the activity itself as well as attempting to run my SurfaceView within a boolean run but neither seemed to work.

Any ideas would be appreciated!

Bedimindtricks
  • 113
  • 1
  • 7

2 Answers2

2

You can create a flag like boolean in your SurfaceView class like isRunning=false; then check it in your before your start your thread. also create two methods onPause() and onResume() in surface view.

onPause(){
   isRunning=false;
   // make thread null;
}
And
onResume(){
isRunning=true;
}

Call both these methods in your activities onPause() and onResume() method receptively.

Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
  • What I have now is my SurfaceView wich uses my GameLoopThread. I attempt to set my GameLoopThread boolean to false (not running) when I enter the pause button, but when I click on my resume button (set GameLoopThread boolean to true) to re-run my thread it does nothing. – Bedimindtricks Nov 27 '13 at 09:24
  • 1
    have u implemented the onPause() and onResume() methods of Activity class, and called SurfaceView's onPause() and onResume() there? – Virag Brahme Nov 27 '13 at 09:33
  • Oh, I did not know I needed the onPause() and onResume() methods in my SurfaceView. Would these just include super.onPause() and super.onResume()? – Bedimindtricks Nov 27 '13 at 09:36
  • No.. those are your own methods.. and call them in your activity.. using mSuraceView.onPause() in your Activitiy's onPause() method – Virag Brahme Nov 27 '13 at 09:41
  • Yes that is my problem though, I am not sure what should be used within my onPause() method in my SurfaceView. I am unsure how to pause it. – Bedimindtricks Nov 27 '13 at 09:43
  • 1
    may this help http://stackoverflow.com/questions/7068443/restarting-pausing-thread-in-onresume-onpause?rq=1 – Virag Brahme Nov 27 '13 at 09:46
  • I have seen that and tried a few of the solutions with no success. I will give it another shot though, thank you. – Bedimindtricks Nov 27 '13 at 09:51
  • this may also help http://stackoverflow.com/questions/4059526/programming-with-surfaceview-and-thread-strategy-for-game-development?rq=1 – Virag Brahme Nov 27 '13 at 09:54
  • I will have a look although I believe I have tried a few of these already. Thank you. – Bedimindtricks Nov 27 '13 at 10:19
0

Have you had a look at this?

How to pause/resume thread in Android?

Basically, you can use wait() and notifyAll() in combination with a lock object.

Community
  • 1
  • 1
aysonje
  • 2,595
  • 1
  • 14
  • 16