I'm writing a game using two different SurfaceView's (RollView and
CupView) in two different activities. Both SurfaceView's heavily
resemble the LunarLander demo.

The RollView SurfaceView is working fine. When leaving the Activity,
RollView's surfaceDestroyed() is called, killing the thread. If I then
re-enter that Activity, the thread is recreated in RollView's
constructor and then started in surfaceCreated().

However in CupView the thread never seems to go away. SurfaceDestroyed
is being called, but upon reentering the Activity I get the following
error:

java.lang.IllegalThreadStateException: Thread already started.

This thrown is in surfaceCreated() at the thread.start() call.

The difference between the two, and apparently the problem, is that in
the RollView Activity I call finish() when done. When leaving the
CupView Activity, I'm doing a startActivityForResult
(i,ClassWithRollView.class);......not calling finish().

This is because after leaving the RollView, I want to return to the
CupView. Below is relevant code:

   /** This where I'm exiting the Activity with the CupView **/
    private void rollDice(){
        if (mMode==IN_PROGRESS){
                // create a new intent for the roll dice view
                Intent i = new Intent(this,RollDice.class);
                        mRollCount = mRollCount + 1;
                        if (mRollCount == 3){
                                mTurnCount += 1;
                        }
                        if (mTurnCount == 13){
                                setState(GAME_OVER);
                        }
                i.putExtra("my.package.RollCount", mRollCount);
                i.putExtra("my.package.GameID",mGameID);
                startActivityForResult(i,ROLL_DICE_ACTIVITY);
        }
    }

/** This is CupView's surfaceCreated/Destroyed **/
        
//---------------------------------------------------------------------------------------------
        /* Callback invoked when the Surface has been created and is read to
be used. */
        public void surfaceCreated(SurfaceHolder holder){
                // start the thread here so that we don't busy-wait in run()
                // waiting for the surface to be created
                Log.d("MyApp","cup view surface created");
                thread.setRunning(true);
                thread.start();
        }
        
//---------------------------------------------------------------------------------------------
        /* Callback invoked when the Surface has been destroyed and must no
longer be touched.
         * WARNING: after this method returns, the Surface/Canvas must never
be touched again!
         * (Unless its REALLY asking for it.)
         */
        public void surfaceDestroyed(SurfaceHolder holder){
                // we have to tell thread to shut down & wait for it to finish, 
or
else it might touch
                // the Surface(wouldn't want that!) after we return and explode
(ahhh!).
                Log.d("MyApp","cup view surface destroyed");
                boolean retry = true;
                thread.setRunning(false);
                while (retry){
                        try{
                                thread.join();
                                retry = false;
                        }catch(InterruptedException e){

                        }
                }
        }

So why is the thread saying its already started when surfaceDestroyed
() has been called?

A possible workaround is to call finish() on the CupView Activity in
the rollDice() method above. This doesn't seem to follow common
paradigm though. Any other solutions, comments? All help is
appreciated.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to