Menu option starts Thread - menu hangs.

by Ikon » Thu, 12 Mar 2009 22:00:31 GMT

Sponsored Links

I am developing a game based on the SurfaceView/LunarLander example in
the sample code.
I have one activity, with one menu option that is "new game".

My activity listens for the menu selection, and once new game is
selected, doStart of the Game thread is called.  The game starts up
after 3-4 seconds, but the menu option sticks around on the screen
until the game has started.  I think the main thread is waiting on the
doStart to do its thing, but since it's creating a new thread, how do
I get it to just start the thread and continue its business of hiding
the menu option right away, instead of after 3 seconds?


Menu option starts Thread - menu hangs.

by Streets Of Boston » Thu, 12 Mar 2009 22:28:46 GMT

 Could you provide a code-snippet showing how you coded the menu-
handling and the starting of your thread?


Sponsored Links

Menu option starts Thread - menu hangs.

by Ikon » Fri, 13 Mar 2009 09:48:49 GMT


Here is the snippet where I do the menuHandling:

    public boolean onOptionsItemSelected(MenuItem item) {

        switch( item.getItemId()){
        case START_GAME:
                return true;
        case STOP_GAME:
            return true;
        case PAUSE_GAME:
            return true;
        case RESUME_GAME:
            return true;

        return super.onOptionsItemSelected(item);

Here is my doStart method:

    public void doStart() {
        synchronized (mSurfaceHolder) {

                shape[0].visible = true;
                score = 0;

            mLastTime = System.currentTimeMillis() + 100;


On Mar 12, 10:28am, Streets Of Boston <>


Menu option starts Thread - menu hangs.

by Streets Of Boston » Fri, 13 Mar 2009 10:52:24 GMT

 'm assuming that gameThread is something that has its own thread and
runs the game. And the main thread (on which onOptionsItemSelected is
running) is seperate and handles the user-interface.

If so, the it may be that createShapes() is pretty expensive and takes
a while. This'll hang the menu-option item until it's done and ready.

You could startup the game thread in yet another thread.
This other thread could be your gameThread. But i don't know its
interface and whether it has something that does except jobs/tasks,
e.g. gameThread.submitTask(new Runnable() { public void run()
{ dosomething } }); Let's assume that it doesn't and this submitTask
on gameThread does not exist.

Then you have to create another thread to do this. I would use the
java.util.concurrent package for this:

in your activity you declare an instance variable:
private ExecutorService mGameStarter;

in your onCreate of your activity:
mGameStarter = Executors.newSingleThreadExecutor();

in your onDestroy:

And in your onOptionsItemSelected, inside your case START_GAME
mGameStarter.submit(new Runnable() {
public void run() {

Every time you call submit() the new thread (hed by mGameStarter) will
wake up and execute the Runnable you specified when calling submit().
When the Runnable ends, this thread will sleep until you submit yet
another task.

On Mar 12, 9:48pm, Ikon <> wrote:

Menu option starts Thread - menu hangs.

by Ikon » Fri, 13 Mar 2009 12:02:29 GMT

 hanks for the comprehensive response - that is very helpful!
What I dont understand is that if I am just telling another thread to
go do processing, why would THAT thread not block the main thread

On Mar 12, 10:52pm, Streets Of Boston <>

Menu option starts Thread - menu hangs.

by Ikon » Fri, 13 Mar 2009 12:05:31 GMT

  guess the reason is that ExecutorService.submit must be doing
something different than just starting up a new thread?

On Mar 12, 10:52pm, Streets Of Boston <>

Other Threads

1. Porting Dalvik in Eclair


Is there a readme to port Dalvik in Eclair to other Architectures (say MIPS)



2. ADP 2 availability Belgium

Simple question, is the Android Developer Phone 2 shipping to Belgium.
I can't find a list of countries anywhere.
And if they are shipping to Belgium, what is the shipping costs for
the phone.

Thanks in advance

3. Downgraded my ADP1 to RC29 by mistake! Help!

4. Nx1 (2nd

5. My app is missing in HTC tattoo market

6. how to verify whether an application is lauched or not

7. setContentView fails when creating a new activity