Android -- onAttachedToWindow()


onAttachedToWindow在Activity生命周期中的位置

放大招:

http_staticfree.info_~steve_complete_android_fragment_lifecycle.svg

onAttachedToWindow不能在onCreate中喲~

View view = getWindow().getDecorView();  
WindowManager.LayoutParams lp = (WindowManager.LayoutParams)view.getLayoutParams();  
lp.gravity = Gravity.CENTER;  
lp.width = (dm.widthPixels * 4) / 5;  
lp.height = (dm.widthPixels * 4) / 5;  
getWindowManager().updateViewLayout(view,lp);

放到onCreate中進行測試,結果在lp.gravity = Gravity.CENTER;這行報了空指針異常,所以view.getLayoutParams()獲取的LayoutParams是空。

public final class ActivityThread {    
    ......    
    
    final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {    
        ......    
    
        ActivityClientRecord r = performResumeActivity(token, clearHide);    
    
        if (r != null) {    
            final Activity a = r.activity;    
            ......    
    
            // If the window hasn't yet been added to the window manager,    
            // and this guy didn't finish itself or start another activity,    
            // then go ahead and add the window.    
            boolean willBeVisible = !a.mStartedActivity;    
            if (!willBeVisible) {    
                try {    
                    willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(    
                            a.getActivityToken());    
                } catch (RemoteException e) {    
                }    
            }    
            if (r.window == null && !a.mFinished && willBeVisible) {    
                r.window = r.activity.getWindow();    
                View decor = r.window.getDecorView();    
                decor.setVisibility(View.INVISIBLE);    
                ViewManager wm = a.getWindowManager();    
                WindowManager.LayoutParams l = r.window.getAttributes();    
                a.mDecor = decor;    
                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;    
                ......    
                if (a.mVisibleFromClient) {    
                    a.mWindowAdded = true;    
                    wm.addView(decor, l);    
                }    
            }     
    
            ......    
        }    
    
        ......    
    }    
      
    ......    
}

原來在ActivityThread執行handleResumeActivity時就會為PhoneWindow(r.activity.getWindow)中的DecorView設置LayoutParam,並且通過源碼發現handleResumeActivity函數首先會執行performResumeActivity,此時會調用Activity的onResume()生命周期函數,這時問題就比較清晰了,看來只要在Activity的onResume生命周期后就能獲取DecorView的LayoutParam,進而可以設置高度和寬度了。根據上面貼出的生命周期圖,onResume()后面是onAttachedToWindow(),並且onAttachedToWindow只會調用一次,不會受用戶操作行為影響。所以在onAttachedToWindow中進行窗口尺寸的修改再合適不過了。

干貨

  • onAttachedToWindow運行在onResume之后;
  • DecorView的LayoutParams是在ActivityThread的handleResumeActivity中設置的,並且該函數會調用Activity的onResume生命周期,所以在onResume之后可以設置窗體尺寸;

我是天王蓋地虎的分割線

 

 

參考:http://blog.csdn.net/guxiao1201/article/details/41517871


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM