Android: restore focus using SurfaceView

I am currently dealing with Android, playing with the Lunar Lander pattern.

I found that if you switch from the application (for example, press the call button), it will destroy the underlying surface (call surfaceDestroyed).

Moving backward (which will cause onWindowVisibilityChanged), the application will crash as it tries to draw on the surface without re-creating.

Is there any code I can add to onWindowVisibilityChanged(or somewhere else) that will restore the underlying surface of the SurfaceView and beautifully resume execution?

It seems like it should be a simple function call, but I cannot find anything in the API docs.

Thanks!

+5
source share
2 answers

Mis-diagnosis! The application automatically creates a surface automatically, but there is a call that tries to draw before it is created.

Fixing problems:

boolean mSurfaceExists;
...
public void surfaceDestroyed(SurfaceHolder holder) {
    mSurfaceExists = false;
    ...
}

public void surfaceCreated(SurfaceHolder holder) {
    mSurfaceExists = true;
    ...
}

protected void onWindowVisibilityChanged(int visibility) {
    // only call base if there a surface
    if(mSurfaceExists)
        super.onWindowVisibilityChanged(visibility);
}

Now everything is swelling. (as far as I know, in any case, Java / Android experts are not shy about commenting if this is bad practice!)

+1
source

This solution for "mSurfaceExists = true" does not work for me either. It seems that the surfaceCreated () function is not getting called because super.onWindowVisibilityChanged () is not being called. Thus, the surface is not created, and it does not fall, because threas.start is not called.

, : thread.start() , . surfaceDestroyed() thread.join . .

, , surfacecreated , (back key) ( ). , isFinishing() .

, . .

+2

All Articles