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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:16:45+00:00 2026-06-03T12:16:45+00:00

After finding that starting a new intent might not be the right way to

  • 0

After finding that starting a new intent might not be the right way to notify user of GameOver, I’m now struggeling with runOnUiThread, Runnable and dialogs..

Question : How and where would I implement a Dialog.show() to notify the user that the game has ended? In my Log, it all works fine (so I managed to get the message Game end when it’s supposed to). At some point, boolean GameManger.LevelEnded switches to true. When that happens, I want to show the Dialog..

I’ve experimented with runOnUiThread, Runnable and Async threads. Can’t figure it out.

I have a game. Activity AppelzActivity :

public class AppelzActivity extends Activity {
    /** Called when the activity is first created. */

    private static final String TAG = AppelzActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(null);

        // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // set our MainGamePanel as the View
        setContentView(new MainGamePanel(this));
        Log.d(TAG, "View added");
    }
}

My MainGamePanel class :

public class MainGamePanel extends SurfaceView implements
SurfaceHolder.Callback {

// DECLARING VARS (REMOVED FOR READABILITY)

    public MainGamePanel(Context context) {
        super(context);
        getHolder().addCallback(this);
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
        .getDefaultDisplay();
        GameManager.panel = this;

        width = display.getWidth();
        height = display.getHeight();
        RectScreen.set(0,0,width,height);

        // CREATING CONTROLS, MISSION AND VISIBLE OBJECTS (REMOVED FOR READABILITY)

        thread = new MainThread(getHolder(), this);
        setFocusable(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread.setRunning(true);
    }

        // HANDELING ON TOUCH EVENTS AND SUCH (REMOVED FOR READABILITY)


    @Override
    protected void onDraw(Canvas canvas) {
        // fills the canvas with black
        canvas.drawColor(Color.BLACK);
        backGnd.draw(canvas);

        basket.draw(canvas);
        for (int i = 0; i < AppleList.size();i++){
            AppleList.get(i).draw(canvas);
        }
        btnMoreFruit.draw(canvas);
        btnLessFruit.draw(canvas);

        if (GameManager.count){

            glyphsCount.drawString(MainThread.canvas, Integer.toString(GameManager.i), (width/2-120), (height/2-120));

        }

    }
}

And finally, the MainThread class :

public class MainThread extends Thread {

    private static final String TAG = MainThread.class.getSimpleName();

    private SurfaceHolder surfaceHolder;
    private MainGamePanel gamePanel;
    private DialogManager dialogManager;
    public static boolean running;
    public static Canvas canvas;

    public void setRunning(boolean running) {
        MainThread.running = running;
    }

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

    @Override
    public void run() {
        // Canvas canvas;
        Log.d(TAG, "Starting game loop");
        while (running) {
            MainThread.canvas = null;
            // try locking the canvas for exclusive pixel editing on the surface

            try {
                MainThread.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.onDraw(MainThread.canvas);

                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (MainThread.canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(MainThread.canvas);
                }
            } // end finally
        }
    }
}

Basket.java – This is an object in the game. As you can see, if it is in a certain position, I start a CountDown :

public class basket {

 private Bitmap bitmap; // the actual bitmap
 private int x;   // the X coordinate
 private int y;   // the Y coordinate'
 public static int width;
 public static int height;
 public float X = 100;
 public float Y = 100;

 private boolean touched; // if droid is touched/picked up

 public basket(Bitmap bitmap, int x, int y) {
  this.bitmap = bitmap;

  width = bitmap.getWidth(); 
  height = bitmap.getHeight(); 
 }

 public Bitmap getBitmap() {
  return bitmap;
 }
 public void setBitmap(Bitmap bitmap) {
  this.bitmap = bitmap;
 }

 public int getWidth(){
      return bitmap.getWidth();
     }

     public int getHeight(){
      return bitmap.getHeight();
     }

 public int getX() {
  return x;
 }



 public void setX(float x) {

    // if basket is further than slotmargin 
     if (x + width/2 > SlotManager.SlotmarginX) {
        // if basket is NOT in CheckOut area    
         if (this.Y < MainGamePanel.height - 80 - height/2){
            x = SlotManager.SlotmarginX - width/2;

        } else {
            // if basket IS in CheckOut area
            x = MainGamePanel.width - width/2;

            // Start the countdown timer if not already running
            if (!GameManager.count){
                GameManager.count=true;
// --- HERE I START COUNTDOWN
                GameManager.handler.postDelayed(GameManager.runnable, 0);                               
            }

        }
    } else {

        // Basket is NOT in CheckOut area; stop countdown timer
        GameManager.count=false;
    }

     this.X = x;
 }


 public int getY() {
  return y;
 }
 public void setY(int y) {
        if (y + height/2 > MainGamePanel.height - 80){
            y = MainGamePanel.height - 80 - height/2;
        }


  this.Y = y;
 }


 public boolean isTouched() {
  return touched;
 }

 public void setTouched(boolean touched) {
  this.touched = touched;
 }

 public void draw(Canvas canvas) {
      canvas.drawBitmap(bitmap, X - width/2, Y - height/2, null);
 }

 public void handleActionDown(int eventX, int eventY) {
  if (eventX >= (X - width/ 2) && (eventX <= (X + width/2))) {
   if (eventY >= (Y - height/ 2) && (eventY <= (Y + height/ 2))) {
    // basket touched
    setTouched(true);
   } else {
    setTouched(false);
   }
  } else {
   setTouched(false);
  }

 }
}

The CountDown in GameManager is :

        @Override
        public void run()
        {
            if (SlotManager.alSlottedFruitList.size() > 0){
            //TODO Create Full screen countdown timer
                Log.d("", "Countdown Timer in GameManager " + i);

                i--;
                if (count && i > -1) // stop timer when count is false
                {
                    handler.postDelayed(runnable, 1000);
                } else {
                    if (i == -1){
                        Log.d("", "RoundUp the Level");

// --- HERE I WANT TO DO A LEVEL END AND SHOW THE DIALOG                        

                    }
                    // reset Countdown (if basket outside checkout)
                    i = 6;
                }
            } else {
                // reset Countdown (if slot has no fruits)
                i = 6;
            }
        }
    };

    public static void startCountDown() {
        handler.postDelayed(GameManager.runnable, 0);
    }

    public static void finishRound() {
        listener.onRoundFinished();
    }



    public static List<Integer> missionlist;

    public static void CreateMission(){
        int maxtotalitems = 6;
        missionlist = new ArrayList<Integer>();
        Random Rnd = new Random();

        for (int FruitType = 0; FruitType < FruitManager.fruitResources.size();FruitType++){
            for (int x = 0; x < Rnd.nextInt(maxtotalitems); x++){
                missionlist.add(FruitType); 
            }}

// --- HERE I WANT TO SHOW A DIALOG CONTAINING THE MISSION


        for (int x = 0; x < FruitManager.fruitResources.size();x++){
            Log.d("MISSION", "Mission for " + x + " : " + Collections.frequency(missionlist, x));
        }

// Let's show the Dialog containing the mission 

    }

    public void LevelEnd(){
        // First, count the Fruits in the slot
        SlotManager.countSlotInv();
        List<Integer> list = new ArrayList<Integer>();      
        // Fill itemIds with the types of fruits
        for (apple a : SlotManager.alSlottedFruitList){
            Integer itemId = Integer.valueOf(a.FruitType);
            list.add(itemId);
        }


            for (int key = 0; key < 3 ; key++) {
                boolean succeed = CheckMissionComplete(key, Collections.frequency(list, key));

                System.out.println(key + " : " + Collections.frequency(list, key));
                    Log.d("MISSION", "Number : " + key + " : succeed = " + succeed );                   
            }
            listener.onRoundFinished();
    }

    public boolean CheckMissionComplete(int key, int amount) {

        //CheckMission
        if (amount == Collections.frequency(missionlist, key)){
            return true;            
        } else {
        return false;
        }
    }

    public GameManager(GameListener listener) {
        this.listener = listener;
    }





}
  • 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-03T12:16:50+00:00Added an answer on June 3, 2026 at 12:16 pm

    Just try to keep it simple. Hold a reference to the context in your GameManager class. Use it to display your dialog.

    public class MainGamePanel extends SurfaceView implements
                                       SurfaceHolder.Callback {
    
    public MainGamePanel(Context context) {
        super(context);
       .......
        GameManager.panel = this;
        GameManager.context = context; // add a reference to the context in GameManager..
       ........
       ........
    
        thread = new MainThread(getHolder(), this);
        setFocusable(true);
    }
    

    In your GameManager use it to call runOnUIThread()

     Activity context;  // Context of the main activity..
     ........
     @Override
        public void run()
        {
            if (SlotManager.alSlottedFruitList.size() > 0){
            //TODO Create Full screen countdown timer
                Log.d("", "Countdown Timer in GameManager " + i);
    
                i--;
                if (count && i > -1) // stop timer when count is false
                {
                    handler.postDelayed(runnable, 1000);
                } else {
                    if (i == -1){
                        Log.d("", "RoundUp the Level");
    
    // --- HERE I WANT TO DO A LEVEL END AND SHOW THE DIALOG    
    
                        context.runOnUIThread(new Runnable() {   // Use the context here
                            @override
                            public void run() {
                                showLevelEndDialog();
                            }
                        }
                    }
                    // reset Countdown (if basket outside checkout)
                    i = 6;
                }
            } else {
                // reset Countdown (if slot has no fruits)
                i = 6;
            }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After finding out the hard way that clone() does not work as intended for
I'm using Silverlight Beta 4 for a LOB application. After finding out today that
I'm finding this kind of overriding after a new in java code very often
I am finding that isAnimating is returning true even after it has completed the
I know that Cross validation is used for selecting good parameters. After finding them,
Problem Scripts on some pages that use jquery are not activating. Scenario Finding that
after finding out that there is a bug in the UITapGestureRecognizer ( How to
In VIM, after finding text with / command, that text remains highlighted. What is
I'm new to WPF and I'm finding that the WPF .Net 4.0 Datagrid doesn't
I'm finding that after my app has been in the background for a while,

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.