Menu option starts Thread - menu hangs.

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


Sponsored Links
 Hi,

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?

Thanks,
Ayan
--~--~---------~--~----~------------~-------~--~----~



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


 Streets,

Here is the snippet where I do the menuHandling:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch( item.getItemId()){
        case START_GAME:
                gameThread.doStart();
                return true;
        case STOP_GAME:
                gameThread.setState(gameThread.STATE_LOSE);
            return true;
        case PAUSE_GAME:
                gameThread.pause();
            return true;
        case RESUME_GAME:
                gameThread.unpause();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

Here is my doStart method:

    public void doStart() {
        synchronized (mSurfaceHolder) {

                createShapes(mContext);
                shape[0].visible = true;
                score = 0;

            mLastTime = System.currentTimeMillis() + 100;
            setState(STATE_RUNNING);
        }
    }

Thanks,
Ayan


On Mar 12, 10:28am, Streets Of Boston <flyingdutc...@gmail.com>



--~--~---------~--~----~------------~-------~--~----~



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:
mGameStarter.shutdownNow();

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


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 <ayanafr...@gmail.com> 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
then?


On Mar 12, 10:52pm, Streets Of Boston <flyingdutc...@gmail.com>
wrote:
--~--~---------~--~----~------------~-------~--~----~



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 <flyingdutc...@gmail.com>
wrote:
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Struggling with google maps

Hi.

I was following this tutorial: 
http://mobiforge.com/developing/story/using-google-maps-android
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="com.google.android.maps" /> as a child element to my
aplication and
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"></uses-
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.

Hi,

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

linearlayout.removeAllViews();
linearlayout.addview(listview)
scrollView.addView(linearlayout);

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