是否可以手动销毁SurfaceView? [英] Is it possible to manually destroy SurfaceView?

查看:901
本文介绍了是否可以手动销毁SurfaceView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SurfaceView没有被破坏掉了,即使活动的onPause被调用。

My SurfaceView is not getting destroyed even if onPause of the activity is called.

我照顾的线程在

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) {
        }
    }
}

作为一个黑客我要检查在onResume,如果线程已经结束,我会完成该活动的线程的状态

As an hack I have to check the state of the thread in onResume and if the thread is already terminated, I would finish the activity

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

}

不幸的是不能移动线程处理来自surfaceDestroyed和surfaceCreated到的onPause()和活性onResume()。是否有可能破坏手动在的onPause(中SurfaceView)和onResume重新创建()?

Unfortunately it is not possible to move the thread handling from surfaceDestroyed and surfaceCreated to onPause() and onResume() of the activity. Is it possible to manually destroy the SurfaceView in the onPause() and recreate it in onResume()?

推荐答案

您可以添加一个布局的surfaceview的父母,在的onPause()走了,并在onResume(的设置可见)的布局,然后设置可见活动。

You can add a layout as a parent of the surfaceview, then set visibility of the layout GONE in onPause(), and set VISIBLE in onResume() of the activity.

这篇关于是否可以手动销毁SurfaceView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆