安卓:使用SurfaceView重获焦点 [英] Android: Regaining focus using SurfaceView

查看:405
本文介绍了安卓:使用SurfaceView重获焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前掌握的Andr​​oid,与月球着陆器样品玩耍。

I'm currently getting to grips with Android, playing around with the Lunar Lander sample.

我发现,如果从应用程序导航离开(如,打的呼叫按钮),它会破坏下垫面(呼叫 surfaceDestroyed

I've found that if you navigate away from the app (eg, hit the call button) it will destroy the underlying surface (calling surfaceDestroyed).

导航回(这将触发 onWindowVisibilityChanged )的应用程序会崩溃,因为它会尝试绘制到表面无需重新创建它。

Navigating back (which will trigger onWindowVisibilityChanged) the app will crash, as it will try to draw to the surface without recreating it.

有一些code,我可以在 onWindowVisibilityChanged 放(或其他地方),将重新生成SurfaceView的下垫面,并很好地继续执行?

Is there some code I can put in onWindowVisibilityChanged (or anywhere else) that will regenerate the SurfaceView's underlying surface and resume execution nicely?

这感觉就像这应该是一个简单的函数调用,但我找不到在API文档任何东西。

It feels like this should be a simple function call but I can't find anything in the API docs.

谢谢!

推荐答案

误诊断!该应用程序会自动重新创建表面,但有在那里打电话,试图它的创建之前提请吧。

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.

修复问题:

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

现在这一切都膨胀。 (据我所知,反正 - 的Java / Android的专家随时评论如果这是不好的做法)

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

这篇关于安卓:使用SurfaceView重获焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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