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

  • Home
  • SEARCH
  • 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 7020175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:17:03+00:00 2026-05-27T23:17:03+00:00

What should I do to call thread’s pause() method from showExitDialog() here ? Here’s

  • 0

What should I do to call thread’s pause() method from showExitDialog() here ?
Here’s Start Game class

package game.mainmenu;


import game.view.ViewPanel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class StartGame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );

        setContentView(new ViewPanel(this));      

    }

    @Override
    protected void onPause() {
        super.onPause();
        //saveScores();
        this.finish();
        System.exit(1);// pause game when Activity pauses
    }


    @Override
    public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {

        if (pKeyCode == KeyEvent.KEYCODE_BACK
                && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
            showExitDialog();

            return true;
        }

        return super.onKeyDown(pKeyCode, pEvent);
    }

    public void showExitDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(StartGame.this);
        builder.setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setTitle("EXIT")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                StartGame.this.finish();
                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alert = builder.create();
        //alert.setIcon(android.R.drawable.star_on);
        alert.show();

    }
}

Here’s class with main thread

public class ViewManager extends Thread 
{
    //some states here
    public static final int STATE_LOSE = 1;
    public static final int STATE_PAUSE = 2;
    public static final int STATE_READY = 3;
    public static final int STATE_RUNNING = 4;
    public static final int STATE_WIN = 5;

    //..some not mention code here../

    public ViewManager(SurfaceHolder surfaceHolder, Context context)
    {
        mSurfaceHolder = surfaceHolder;
        mRunning = false;
        mHealthBar = new Rect(0,0,0,0);
        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setARGB(255, 0, 255, 0);
        mLinePaint.setTextSize(16);
        mLinePaint.setStrokeWidth(3);
        mContext = context;
        Resources res = context.getResources();

        //..some not mention code here../
        InitElements(res);

        mHero = new PlayerAnimated(mPlayerImage, FIELD_WIDTH/2, 600, 64, 64, 3, 3, context, mEnemiesList);
        //mBoom = new Explosion(mExplosionImage, 200, 500, 64, 64, 7, 7);
        mEnemyImage = BitmapFactory.decodeResource(res, R.drawable.enemyone);

        setState(STATE_RUNNING);

    }


    /**
        * threads state
        * @param running
        */
       public void setRunning(boolean running)
       {
           mRunning = running;
       }

       //..some not mention code here../

       public void run()
       {
           while (mRunning)
           {
               Canvas canvas = null;
               try
               {
                   // подготовка Canvas-а
                   canvas = mSurfaceHolder.lockCanvas();
                   synchronized (mSurfaceHolder)
                   {
                       if(mMode == STATE_RUNNING){

                           // draw if not paused
                           addEneimes(mContext);

                           updateStuff();

                           doDraw(canvas);
                       }
                       else
                       {
                           pauseDraw(canvas);
                       }
                       ViewPanel.displayFps(canvas, aString);

                       aString = Integer.toString(hudscore.getScore());

                   }
               }
               catch (Exception e) { }
               finally
               {
                   if (canvas != null)
                   {
                       mSurfaceHolder.unlockCanvasAndPost(canvas);
                   }
               }
           }
       }

       //..some not mention code here../

    public void pause() {
        synchronized (mSurfaceHolder) {
           setState(STATE_PAUSE);
           mLastFiredTime = System.currentTimeMillis() + 100;
        }
    }

    public void unpause() {
        //
        synchronized (mSurfaceHolder) {
            setState(STATE_RUNNING);
            mLastFiredTime = System.currentTimeMillis() + 100;
        }
    }

    public void setState(int mode) 
    {
        mMode = mode;
    }

    public void pauseDraw(Canvas canvas)
    {
         canvas.drawBitmap(Bitmap.createBitmap(FIELD_WIDTH, FIELD_HEIGHT, Bitmap.Config.RGB_565), 0, 0, null);
    }
}
  • 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-27T23:17:04+00:00Added an answer on May 27, 2026 at 11:17 pm

    Not really a clear question, since you are not telling us where you want to create and start the Thread in the main code. Let’s assume it’s inside onCreate:

    public class StartGame extends Activity {
    
        private ViewManager viewManager = new ViewManager();
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
    
            setContentView(new ViewPanel(this));    
    
            viewManager.start(); // start viewManager thread
        }
    
        // other methods
    
        public void showExitDialog() {
    
            viewManager.pause(); // call pause
    
            // rest of code
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why i should call Control.Invoke from non-ui thread? As i know any manipulations with
The reference says this: You should call this function from the main thread of
The general advice is that you should not call GC.Collect from your code, but
when overriding the loadView method in UIViewController, should one call [super loadView] in the
Here is a skeleton of my thread class: class MyThread { public: virutal ~MyThread();
I have two threads running from a controller class. The first thread receives SMS
How can I know, whether I should make a function call within GUI thread.
I want to create one method and call the created Thread inside that method
I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing
I have some ctypes bindings, and for each body.New I should call body.Free. The

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.