是否可以手动销毁 SurfaceView?

Gur*_*eva 5 android surfaceview

即使调用了活动的 onPause,我的 SurfaceView 也不会被破坏。

我正在处理线程

public void surfaceCreated(SurfaceHolder holder) {
    if (mGameThread.getState() == Thread.State.TERMINATED) {
        createGameThread(getHolder(), getContext());
    }
    mGameThread.setRunning(true);
    mGameThread.start();
}


public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    mGameThread.setRunning(false);
    while (retry) {
        try {
            mGameThread.join();
            retry = false;
        } catch (InterruptedException e) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

作为一个黑客,我必须在 onResume 中检查线程的状态,如果线程已经终止,我将完成活动

protected void onResume() {
    Log.d(mLogTag, "onResume()");
    super.onResume();
    if (mGameThread != null) {
        if (mGameThread.getState() == Thread.State.TERMINATED) {
            finish();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

不幸的是,不可能将线程处理从surfaceDestroyed 和surfaceCreated 移动到活动的onPause() 和onResume()。是否可以在 onPause() 中手动销毁 SurfaceView 并在 onResume() 中重新创建它?

Muh*_*dil 3

您可以添加一个布局作为 SurfaceView 的父级,然后在 onPause() 中设置布局的可见性为 GONE,并在 Activity 的 onResume() 中设置为 VISIBLE。