Android and androidsdk
OnAttachedToWindow position in Activity Lifecycle
Zoom in:
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 );
Put it in onCreate for testing, and the result is in lp. gravity = Gravity. CENTER; this row reports a null pointer exception, so the LayoutParams obtained by view. getLayoutParams () is null.
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); } } ...... } ...... } ...... }
It turns out that when handleResumeActivity is executed in ActivityThread, it will be PhoneWindow (r. activity. in getWindow), DecorView sets LayoutParam and finds through the source code that the handleResumeActivity function will first execute javasmresumeactivity. At this time, the onResume () Life Cycle Function of the Activity will be called, which makes the problem clearer, it seems that the LayoutParam of DecorView can be obtained after the onResume lifecycle of the Activity, and the height and width can be set. According to the life cycle diagram shown above, onResume () is followed by onAttachedToWindow (), and onAttachedToWindow is called only once, and will not be affected by user operations. Therefore, it is better to modify the window size in onAttachedToWindow.
Dry Goods
- OnAttachedToWindow runs after onResume;
- The LayoutParams of DecorView is set in the handleResumeActivity of ActivityThread, and this function will call the onResume life cycle of the Activity. Therefore, you can set the form size after onResume;
I am the dividing line of tiantiao
Reference: http://blog.csdn.net/guxiao1201/article/details/41517871