How: Start an Activity inside a Thread and use finish() to get back.

Posted by Kirk Becker on Stack Overflow See other posts from Stack Overflow or by Kirk Becker
Published on 2011-02-20T23:02:56Z Indexed on 2011/02/20 23:25 UTC
Read the original article Hit count: 168

Filed under:

Hello,

I am programming a game on android. I'm using a Thread while calling a Surface View class to update and draw my game. Inside the update I wanted to start an activity based on if the game has just started and this would launch my MENUS.

My Thread for the most part..

    while (myThreadRun) {
        Canvas c = null;
        try {
            gameTime = System.currentTimeMillis();
            c = myThreadSurfaceHolder.lockCanvas(null);
            synchronized (myThreadSurfaceHolder) {
                // Update Game.
                myThreadSurfaceView.onUpdate();
                // Draw Game.
                myThreadSurfaceView.onDraw(c);

You can see there where I am updating the game... here is onUpdate();

protected void onUpdate() {

    // Test if menu needs to be displayed.
    while (thread.getMenu()) {
        // Test if menu activity has been started.
        if (thread.getMenuRunning() == false) {
            Intent menuIntent = new Intent(this.getContext(), MyMenu.class);
            ((Activity) cxt).startActivityForResult(menuIntent, 1);
            thread.setMenuRunning(true);
        }
    }

I am using a while loop because if I didn't use it the thread just keeps going.

Basically I just don't know how to implement my menus using a thread as a game loop. Everywhere I look it seems like that's best practice.

In my menu activity I just display the menu layout and a few buttons and when the person wants to start the game it uses finish() to go back to my thread where they play the game.

I am very new to this so any insight will be helpful,

Thanks

© Stack Overflow or respective owner

Related posts about android