0

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..

andro-girl
  • 7,989
  • 22
  • 71
  • 94

3 Answers3

0

you need to call dismiss() of dialog instead of cancel().

dialog.dismiss();

try this one

Pratik
  • 30,639
  • 18
  • 84
  • 159
0

For each condition you are cancelling the dialog.So I think you should not check for cancelling.You can do it like the following.

alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                        if(!mainscreenOn)
                        {
                             GamePanel.thread.setRunning(true);
                             GamePanel.thread.run();
                        }


                    return;
                }}); 

Edited:

Then try with Positive Button and Negative Button.If it does not work then Check the point which is mentioned by "Yugandhar Babu".

AlertDialog.Builder builder = new AlertDialog.Builder(this);;
                builder.setMessage("Are you sure you want to exit?")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                finish();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                alert.show();
jainal
  • 2,973
  • 2
  • 20
  • 18
0

I tested your code without GamePanel statements, its working fine for both mainscreenOn equal to true and flase.

I think in your case its not returning from onClick() when mainScreenOn is false, means its strucking in GamePanel.thread.setRunning() or GanePanel.thread.run().


Check this link it may help you. about pause and resume of thread.

How to pause/resume thread in Android


You are calling run() after setRunning(true). I think if your thread has Runnable object then only you have to call run(). as below

Thread t = new Thread(new Runnable() {
      public void run() {
          // your task
      }
});

If you are not using Runnable object, i think no need to call run(). anyhow you are controlling thread with running variable.

Please check this, i too not confident about it.

Community
  • 1
  • 1
Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
  • i know it is the problem of calling thread from here..but i dnt knw how to fix it... – andro-girl Dec 22 '11 at 06:30
  • I thought you don't know this reason, sorry i can't help you to fix your problem. bye – Yugandhar Babu Dec 22 '11 at 06:32
  • if i am not calling run() methoed then how can i call that from this?i have tried with only setRunning(true) statement.but it is not working.. – andro-girl Dec 22 '11 at 07:52
  • You are calling run() method, means you have to wait until it returns. But in your run() method infinite loop is there. If you break that loop its working fine for pressing NO button on alert dialog. I think whatever you did for pause and resume of thread is not right way. check for other alternative. – Yugandhar Babu Dec 22 '11 at 09:32