1. 程式人生 > >實習入職第二天:onAttachedToWindow () 和 onDetachedFromWindow ()

實習入職第二天:onAttachedToWindow () 和 onDetachedFromWindow ()

在重寫View的時候,會遇到這兩個方法

protected void onAttachedToWindow()

Description copied from class: View

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before View.onDraw(Android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after View.onMeasure(int, int).
Overrides: 
onAttachedToWindow in class View

當此view附加到窗體上時呼叫該方法。在這時,view有了一個用於顯示的Surface,將開始繪製。注意,此方法要保證在呼叫onDraw(Canvas) 之前呼叫,但可能在呼叫 onDraw(Canvas) 之前的任何時刻,包括呼叫 onMeasure(int, int)之前或之後。

看得出次方法在onDraw方法之前呼叫,也就是view還沒有畫出來的時候,可以在此方法中去執行一些初始化的操作,google的AlarmClock動態時鐘View就是在這個方法中進行廣播的註冊,程式碼如下:

  1. @Override
  2.     protectedvoid onAttachedToWindow() {  
  3.         super.onAttachedToWindow();  
  4.         if (Log.LOGV) Log.v("onAttachedToWindow " + this);  
  5.         if (mAttached) return;  
  6.         mAttached = true;  
  7.         if (mAnimate) {  
  8.             setBackgroundResource(R.drawable.animate_circle);  
  9.             /* Start the animation (looped playback by default). */
  10.             ((AnimationDrawable) getBackground()).start();  
  11.         }  
  12.         if (mLive) {  
  13.             /* monitor time ticks, time changed, timezone */
  14.             IntentFilter filter = new IntentFilter();  
  15.             filter.addAction(Intent.ACTION_TIME_TICK);  
  16.             filter.addAction(Intent.ACTION_TIME_CHANGED);  
  17.             filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);  
  18.             mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);  
  19.         }  
  20.         /* monitor 12/24-hour display preference */
  21.         mFormatChangeObserver = new FormatChangeObserver();  
  22.         mContext.getContentResolver().registerContentObserver(  
  23.                 Settings.System.CONTENT_URI, true, mFormatChangeObserver);  
  24.         updateTime();  
  25.     }  

另外在遮蔽Home鍵的時候也會用到

  1. publicvoid onAttachedToWindow() {  
  2. this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);  
  3. super.onAttachedToWindow();  
  4. }  


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

protected void onDetachedFromWindow()

Description copied from class: View
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
Overrides: 
onDetachedFromWindow in class AdapterView<ListAdapter>

將檢視從窗體上分離的時候呼叫該方法。這時檢視已經不具有可繪製部分。

onDetachedFromWindow()正好與onAttachedToWindow()的用法相對應,在destroy view的時候呼叫,所以可以加入取消廣播註冊等的操作,還是google的鬧鐘程式碼:

  1. @Override
  2.     protectedvoid onDetachedFromWindow() {  
  3.         super.onDetachedFromWindow();  
  4.         if (!mAttached) return;  
  5.         mAttached = false;  
  6.         Drawable background = getBackground();  
  7.         if (background instanceof AnimationDrawable) {  
  8.             ((AnimationDrawable) background).stop();  
  9.         }  
  10.         if (mLive) {  
  11.             mContext.unregisterReceiver(mIntentReceiver);  
  12.         }  
  13.         mContext.getContentResolver().unregisterContentObserver(  
  14.                 mFormatChangeObserver);  
  15.     }  


具體的用法視個人的需求而定了,自己控制重寫就好了。