Android: Regaining focus using SurfaceView

[亡魂溺海] 提交于 2019-12-05 09:53:12

This solution for "mSurfaceExists = true" isn't working for me either. It looks like surfaceCreated() is not getting called because super.onWindowVisibilityChanged() is not called. So there is no surface created and it doesn't crash because threas.start is not called.

I believe the problem is: Calling thread.start() causes the error because the thread was already started. But in surfaceDestroyed(), thread.join causes the thread to complete and die. And a thread can not be restarted once dead.

I am guessing the trick is to create a new thread in surfacecreated or to only cause the thread to complete when user is calling the application to finish(back key) and not pausing(home key). This can be checked by calling isFinishing() on the activity.

Not sure if this will work. I will be trying this soon.

Mis-diagnosis! The app re-creates the surface automatically, but there's a call in there that tries to draw to it before it's created.

Fixing the problem:

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's a surface
    if(mSurfaceExists)
        super.onWindowVisibilityChanged(visibility);
}

Now it's all swell. (as far as I'm aware, anyway - Java/Android experts feel free to comment if this is bad practise!)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!