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. Problem invoking View Intent for Messaging App

I am trying to view a message using the Messaging application on the
emulator. I created a message using Telnet and can see the message in
the Messaging App. I tried to invoke the view intent using the
following code. When this code runs the Messaging App starts but
displays the UI panel with the data as if I was a composing a message
rather than viewing the message.

  Uri uri = Uri.parse("sms:1234567890");
  Intent intent = new Intent(Intent.ACTION_VIEW,uri);
  intent.putExtra("sms_body", "testsmsmessage");
  intent.putExtra("compose_mode", false);

What am i doing wrong?


2. The name of the contacts group

I found the name of the contacts group can not contain a slash "/"
character. Otherwise, Android can't sync correctly(It says "The XXXX
group is empty"). Is it a bug or a rule?


3. ADP1 not listed in ubuntu by adb

4. no xml for PathShape?

5. Eclipse plugin 0.9

6. Howot insert icons, styling text in TextView

7. Need info to get the debug trace of the opencore