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. youtube API

hi all,

is there have youtube API or lib (like jar file) in the Android?
I hope that can write a app like youtube on the htc mobile phone,
such that I can search video, and return the video information
(snapshot,video length,shard author..etc)

can somebody give me some suggestion?


2. Android search in pictures (Informational post)

As I have been looking into the android quick search box, here is a
series of pictures that digs into the nature of the new Android

These pictures should be helpful as you read through the Android docs on search



3. MapView.postInvalidate() triggers redundant call to onDraw()?

4. device shape configuration

5. Market style buttons

6. How to add external static libraries to

7. :Training on Advance C and Linux System Programming from Expert