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. Struggling with google maps


I was following this tutorial:
on creating map activities, Now i am unable to understand why my
application crashes. I get a NoClassDefFoundError and that exception
is caused by a  Caused by: java.lang.IllegalAccessError: cross-loader
access from pre-verified class.

Now i have added the <uses-library
android:name="" /> as a child element to my
aplication and
permission> and
<uses-permission android:name="android.permission.INTERNET"/>
as children of the Manifest element.

My avd is running on api level 4 with the google apis.

What is wrong? i have searched the web, but nnot found a solution to
my problem.

Appreciate any thoughts or comments on what might be wrong....

2. How to add scrollview to linearlayout programtically.


   I am trying to add scrollview to linearlayout but it showing error
"ScrollView can have only one direct child".. I tried like this
Scrollview scrollview = new ScrollView(context)


Can anybody point out where i am doing mistake.

3. Chart API

4. prting procedure for Android on SMDK2440 board

5. Changes in API in 1.5 - onFreeze and friends

6. fatal: Out of memory, malloc failed fatal: index-pack failed

7. Using Android on "old" palm and dell handhelds