天天看点

surfaceDestroyed什么时候被调用

    今天看别人的代码,突然有个疑问,surfacedestroyed这个函数什么时候被调用呢?

    上网搜了一番,基本都说是surface被销毁的时候,才会调用surfacedestroyed。问题又来了surface什么时候被销毁呢,大家都知道surface是surfaceview的一个专门用于绘图的接口,所以当surfaceview的对象销毁时,surface一定也销毁,这时surfacedestroyed一定会调用。

    可是,我看的那个程序明明没有销毁surfaceview了啊。分步调试了一下结果发现一个现象:

有两个继承surfaceview的类各自是 sva和svb这连个类分别定义了各自的对象aview和bview。如果如今activity设置的是aview这个对象,也就是说如今用aview的surface呢。如果这时在activity运行一下代码:

setcontentview(bview);

这时,aview中的surfacedestroyed就会被调用,由于如今bview的surface要接管aview中的surface,aview的surface就会销毁,这时必定要调用a的surfacedestroyed函数。

    在这里仅仅是说明一种surfacedestroyed被调用的情况,由于个人能力有限,仅仅能写到自己发现的一些东西。这时第一次在论坛上写一些东西,希望以后能坚持下去,在接受别人帮助的同一时候来帮助别人。

    最后把android帮助文档surfacedestroyed调用条件,老外写的简单易懂的原文发过来。

    this is called immediately before a surface is being destroyed. after returning from this call, you should no longer try to access this surface. if you have a rendering thread that directly accesses the surface, you must ensure that thread is no longer touching the surface before returning from this function