今天看別人的代碼,突然有個疑問,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