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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:54:09+00:00 2026-06-11T01:54:09+00:00

Im trying to start a new intent from my gameView using this method: if(happy.getHP()

  • 0

Im trying to start a new intent from my gameView using this method:

    if(happy.getHP() <= 0){ //if my main-character dies
            thread.setRunning(false);
            Context context = getContext();
            Intent intent = new Intent("nielsen.happy.activities.ENDSCREEN");
            context.startActivity(intent);

}

If I do it without thread.setRunning(false);, the endScreen comes up but its buttons wont work, but if i stop my mainThread they do work.

Anyway, the problem with this code is that when my character dies, it freezes for 3-4 seconds, then the endscreen flickers for a sec, then the gameView flickers for a sec, THEN the endscreen comes up for real and the buttons work.

When I start an Activity from another activity, like when I press “Start Game” in my menu, I dont get this problem. I really dont understand what happens here that makes it lag like this.

adding my thread below:

    public void run() {
    Canvas canvas;
    Log.d(TAG, "Starting game loop");
    // initialise timing elements for stat gathering
    initTimingElements();

    long beginTime;     // the time when the cycle begun
    float time1 = System.currentTimeMillis();
    float time2;
    //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 

    sleepTime = 0;

    while (running) {
        canvas = null;
        // try locking the canvas for exclusive pixel editing
        // in the surface
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                beginTime = System.currentTimeMillis();
                framesSkipped = 0;  // resetting the frames skipped
                // update game state 
                this.gamePanel.update();
                // render state to the screen
                // draws the canvas on the panel
                this.gamePanel.render(canvas);              
                // 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
                    this.gamePanel.update(); // update without rendering
                    sleepTime += FRAME_PERIOD;  // add frame period to check if in next frame
                    framesSkipped++;
                }

                // for statistics
                framesSkippedPerStatCycle += framesSkipped;
                // calling the routine to store the gathered statistics
                storeStats();
            }
        } finally {
            // in case of an exception the surface is not left in 
            // an inconsistent state
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }   // end finally
    }
}

adding whole method below:

    private void checkCollision(Canvas canvas) {
    Rect h1 = happy.getBounds();
    for (int i = 0; i < enemies.size(); i++) {
        Rect e1 = enemies.get(i).getBounds();
        if (h1.intersect(e1)){
            if(enemies.get(i).getX() < controls.pointerPosition.x){
                enemies.get(i).setX(-20);
            }else if (enemies.get(i).getX() > controls.pointerPosition.x){
                enemies.get(i).setX(20);
            }else if(enemies.get(i).getY() < controls.pointerPosition.y){
                enemies.get(i).setY(-20);
            }else if(enemies.get(i).getY() > controls.pointerPosition.y){
                enemies.get(i).setY(20);
            }
            if(enemies.get(i).getCooldown() <= 0){
            happy.damageHP(10);
            score.incScore(-10);
            enemies.get(i).setCooldown();
            }
        }
        if(happy.getHP() <= 0){

            thread.setRunning(false);
            try {
                thread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Context context = getContext();
            Intent intent = new Intent("nielsen.happy.activities.ENDSCREEN");
            context.startActivity(intent);
            //end-screen                           !!!!!!!
        }
        if(enemies.get(i).getHP() <= 0){
            enemies.get(i).death(canvas, enemies);
            score.incScore(5);
            break;
        }   
        for (int j = 0; j < bullets.size(); j++) {
            Rect b1 = bullets.get(j).getBounds();
            if (b1.intersect(e1)) {
                enemies.get(i).damageHP(5);
                bullets.remove(j);
            }
        }
    }
}
  • 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-11T01:54:11+00:00Added an answer on June 11, 2026 at 1:54 am

    I would have to see what your thread is doing to be sure, but I’m pretty sure something like this is happening:

    • You call setRunning(false) which sets some flag in the thread class.

    • You start the new intent but the thread hasn’t stopped yet because it hasn’t gotten to the part where it checks the running flag

    • The thread (which hasn’t paused yet) starts making changes to the gui, this confuses the android UI which thinks that view should be inactive

    • The thread gets to the point where it’s waiting for setRunning(true), and everything is happy again.

    If this is true, you can fix by waiting until the thread is blocked on setRunning(true), then creating the new intent.

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

Sidebar

Related Questions

I'm trying to start a service from my activity like this: startService(new Intent(MyActivity.this, MyService.class));
Hello I am trying to start the android browser from a service using this
I'm trying to start a new activity from a non-activity class. From the main
I'm trying to start an Intent for a navigation: startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(google.navigation:q= + item.getPoint().getLatitudeE6()/1E6
I'm trying to start a new ASP.NET Web Application Project using Visual Studio 2008.
I'm trying to start a new struts 2 project using maven (struts2 blank archetype)
I am trying to start an activity from my main activity. Its not working
I'm trying to start a new Activity from a PreferenceActivity. However, it fails with
I'm trying to start an Activity in my AsyncTask from the doInBackground() method but
I'm trying to take a picture from the Camera using intent with onActivityResult but

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.