Android and androidsdk

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.