Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6970193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:40:42+00:00 2026-05-27T16:40:42+00:00

I am developing a game.if the user presses device back button while playing the

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:40:43+00:00Added an answer on May 27, 2026 at 4:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am developing game.i am displaying gun object center bottom of the screen.when user
I'm developing a game which is based around the user controlling a ball which
While developing a Facebook game, I'm realizing that I can't yet think of a
i'm developing my first game in silverlight. It's something like 2D labyrinth and user's
I'm developing a question game and I want to change the answer button pressed
I am developing a game in which the user can turn on / off
Hi i am developing game level editor.Currently I am using win32 with directX.But with
I am developing a game for the web. The map of this game will
I'm developing a game for windows for learning purposes (I'm learning DirectX). I would
I'm developing chess game for Android ( androidchess.appspot.com ). If I want to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.