I am developing a game.if the user presses device back button while playing the game thread is pausing and an alert dialog is showing on the screen.if the user does not want to exit the game the game thread should be resume and alert dialog should disappear..but the problem is i could resume the game thread but could not cancel the alert dialog.given below is my back button event code snippet.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MainGamePanel.thread.setRunning(false);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Exit Alert");
// alertDialog.setIcon(R.drawable.appicon);
alertDialog.setMessage("Do you really want to exit the Game?");
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
return;
} });
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(mainscreenOn)
{
dialog.cancel();
}
else
{
dialog.cancel();
GamePanel.thread.setRunning(true);
GamePanel.thread.run();
}
return;
}});
alertDialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
if mainscreenOn flag is true then alert dialog is getting cancelled..but the problem comes if it is false.can anybody help me please...i am stuck on this...
i came to know that it is not the problem of alert dialog...it is the problem of my game thread..given below is my game thread code..
public class GameThread extends Thread {
private static final String TAG = GameThread.class.getSimpleName();
// Surface holder that can access the physical surface
private SurfaceHolder surfaceHolder;
// The actual view that handles inputs
// and draws to the surface
private MainGamePanel gamePanel;
// flag to hold game state
public boolean running;
public void setRunning(boolean running) {
this.running = running;
}
public GameThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
@Override
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
Log.v(""," ->Inside Thread!" + running);
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
this.gamePanel.update();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.render(canvas);
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
}
when i debug the code the control is going to this code and stuck here..