1. 程式人生 > >Android重寫view時onAttachedToWindow () 和 onDetachedFromWindow ()

Android重寫view時onAttachedToWindow () 和 onDetachedFromWindow ()

intent action efault tor null lock 相對 ext.get pre

在重寫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就是在這個方法中進行廣播的註冊,代碼如下:

[java] view plain copy
  1. @Override
  2. protected void 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鍵的時候也會用到

[java] view plain copy
  1. public void 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的鬧鐘代碼:

[java] view plain copy
  1. @Override
  2. protected void 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. }


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

Android重寫view時onAttachedToWindow () 和 onDetachedFromWindow ()