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 8761633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:19:59+00:00 2026-06-13T15:19:59+00:00

I’m working on a Android game using SurfaceView, the game works fine, so I

  • 0

I’m working on a Android game using SurfaceView, the game works fine, so I want to pause it when the user hits HOME key but when getting back the SurfaceView disappears(black background Edit: sometimes the drawing appears) and buttons are inactive in both cases then I get ANR.

public class MainActivity extends Activity {
    private Game game;
    MainPanel mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        game = new Game();
        LoadLevel();
        Init();
        setContentView(R.layout.main);//contains surfaceview and buttons
        mp = (MainPanel) findViewById(R.id.SurfaceView01);
        mp.Init(game,this);
        Button btnTop = (Button) findViewById(R.id.buttonTop);
        Button btnBottom = (Button) findViewById(R.id.buttonBottom);
        Button btnLeft = (Button) findViewById(R.id.buttonLeft);
        Button btnRight = (Button) findViewById(R.id.buttonRight);
        btnTop.setOnClickListener...
    }

    private void Init() {
        ...
    }

    public void LoadLevel() {
        ...
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Tag","Resume");
    }

    @Override
    protected void onPause() {
            super.onPause();
        Log.d("Tag","Pause");
    }
}

public class MainPanel extends SurfaceView implements SurfaceHolder.Callback {

    private MainThread thread;
    private Game game;
    private MainActivity act;

    public MainPanel(Context context, AttributeSet attrs) {
        super(context,attrs);
        getHolder().addCallback(this);
    }

    public MainThread getThread() {
        return thread;
    }

    public void Init(Game game, MainActivity act){
        this.game = game;
        this.act = act;
    }

    public void Move(int dir) {
        ...
    }

    public void update() {
        ...
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (thread == null) {
            thread = new MainThread(getHolder(), this);
            thread.Start();
            thread.start();
        }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        thread.Stop();
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {}
        }
        thread = null;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        //drawing...
    }
}

public class MainThread extends Thread {
    private final static int MAX_FPS = 30;
    private final static int MAX_FRAME_SKIPS = 3;
    private final static int FRAME_PERIOD = 1000 / MAX_FPS;

    private SurfaceHolder surfaceHolder;
    private MainPanel gamePanel;
    private boolean run = false;
    private boolean start = false;

    public MainThread(SurfaceHolder surfaceHolder, MainPanel gamePanel) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }

    public void Start() {
        this.run = true;
        this.start = true;
    }

    public void Pause() {
        this.start = false;
    }

    public void Resume() {
        this.start = true;
    }
    public void Stop() {
        this.run = false;
    }

    @Override
    public void run() {
        Canvas c = null;
        long beginTime;     // the time when the cycle begun
        long timeDiff;      // the time it took for the cycle to execute
        int sleepTime;      // ms to sleep (<0 if we're behind)
        int framesSkipped;  // number of frames being skipped 

        while(run) {
            while(start) {
                try {
                    c = surfaceHolder.lockCanvas(null);
                    synchronized (surfaceHolder) {
                        beginTime = System.currentTimeMillis();
                        framesSkipped = 0;  // resetting the frames skipped
                        // update game state
                        gamePanel.update();
                        // render state to the screen draws the canvas on the panel
                        if(c!=null) gamePanel.onDraw(c);
                        // calculate how long did the cycle take
                        timeDiff = System.currentTimeMillis() - beginTime;
                        // calculate sleep time
                        sleepTime = (int)(FRAME_PERIOD - timeDiff);

                        if (sleepTime > 0) {
                            // if sleepTime > 0 we're OK
                            try {
                                // send the thread to sleep for a short period very useful for battery saving
                                Thread.sleep(sleepTime);
                            } catch (InterruptedException e) {}
                        }

                        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                            // we need to catch up update without rendering
                            gamePanel.update();
                            // add frame period to check if in next frame
                            sleepTime += FRAME_PERIOD;
                            framesSkipped++;
                        }
                    }
                } finally {
                    if (c != null) surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}
  • 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-06-13T15:20:01+00:00Added an answer on June 13, 2026 at 3:20 pm

    Thx to this post why is my runnable giving ANR? I could solve the problem, I isolated the MainThread from the UI thread for further details check the best answer.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported

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.