Hi Mun,

No answer to this yet. What I found however is that it's not that the
SurfaceView does not get created again. Android in fact creates a new
one, however it looks like at the same time also a new holder is
created. As my code however holds a link to the then outdated holder,
it is not notified of the creation.

As a workaround, in my main thread, whenever lockCanvas returns null,
I try to get the surfaceHolder again. This is working fine on an app
that's installed on several thousand phones now across Android 1.5,
1.6 and 2.1

Relevant code of the main thread is below:

                if (!gotSurface) {
                        try {
                                sleep(100);
                        }
                        catch (Exception e){ }
                }

                if (gotSurface) [do your game action];

                Canvas c = null;
                try {
                        c = surfaceHolder.lockCanvas(null);
                        if (c==null) {

                                //Canvas could not be locked, try to get a new
surface holder
                                surfaceHolder=mySurfaceView.getHolder();
                        }
                        else {
                                synchronized (surfaceHolder) {
                                        gotSurface=true;
                                        [Do your draw operation onto Canvas c];
                                }
                        }
                 }
                finally {
                        if (c != null) {
                                surfaceHolder.unlockCanvasAndPost(c);
                        }
                    }

I'm taking care to only execute this code once I can be reasonably
sure that my application is in the state where the surfaceView should
be visible. I put in the sleep(100) part as I noticed that if I try to
get the surfaceHolder in shorter intervals this blocks the application
after a while.

Hope this works for you as well.

Best regards,

Tobias

On May 27, 3:29 pm, Mun <[email protected]> wrote:
> Hi,
>
> Did you manage to find an answer to this yet?
>
> I'm experiencing the same problem such that my surfaceview gets
> destroyed, (after calling setContentView to load another layout) but
> never gets created again!
>
> Thanks,
>
> Mun
>
> On May 9, 10:37 pm, tobias429 <[email protected]> wrote:
>
> > I've built an application that switches between two screens - a
> > LinearLayout and a SurfaceView - via the application.setContentView()
> > method.
>
> > When switching between the two screens, the surface of the SurfaceView
> > gets destroyed, and never created new again.
>
> > Here is what I do:
>
> > During my application, I switch between the two screens via calling
> > setContentView() from my application.
>
> > My SurfaceView starts a thread that renders the view, once the surface
> > is created. This works fine the first time I'm doing this:
> > 1. Create the SurfaceView
> > 2. Get a handle of the surfaceHolder, and add my SurfaceView as a
> > surfaceHolder callback
> > 3. Call setContentView(SurfaceViewScreen) from my app, to bring the
> > SurfaceView screen to the front
>
> > This now nicely calls onSurfaceCreated() in my SurfaceView.
>
> > Based on a user action, I later call setContentView(Layout) to display
> > my standard Layout again. This nicely calls the onSurfaceDestroyed()
> > mehod of my SurfaceView.
>
> > The problem is now, that if I change back again to the SurfaceView via
> > calling application.setContentView(SurfaceView), the surface never
> > gets re-created, i.e. neither does onSurfaceCreated() get called, nor
> > does my view render correctly, as in the according thread
> > surfaceHolder.getCanvas() returns null.
>
> > What am I doing wrong?
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group 
> > athttp://groups.google.com/group/android-developers?hl=en

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

Reply via email to