The approximate relationship between phonewindow,viewroot,activity

Source: Internet
Author: User

http://www.nowamagic.net/academy/detail/50160216

On Android, we all know about activity. But what is the relationship between an activity and a window?

In Activity.java, we can see two variables, namely:

Private Window Mwindow;

Private WindowManager Mwindowmanager;

Where is this variable assigned? You can see the Attach function,

Mwindow = Policymanager. Makenewwindow (this);

Follow up to see, you can find the return of a Phonewindow object, Phonewindow is a derived class of the window class.

Mwindow.setwindowmanager (null, Mtoken, mcomponent.flattentostring ());

if (mparent! = null) {

Mwindow.setcontainer (Mparent.getwindow ());

}

Mwindowmanager = Mwindow.getwindowmanager ();

Following this code carefully we can get the WindowManager object, and this object is unique to the system, and the object is also assigned to the Phonewindow member variable.

When we create a new activity, we often like to use the following code to define the activity's UI interface, so how does this specific implementation come about?

Public void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Main);

We'll know when we follow the Setcontentview. It can be found that the activity actually gave the operation to their own mwindow to complete, by tracking Phonewindow in the Setcontentview function can draw the following knowledge:

1, each Phonewiondow has an object called Decorview, the object is the frame view of the Phonewiondow, you can find such a piece of code:

View in = Mlayoutinflater.inflate (Layoutresource, null);

Decor.addview (in, new viewgroup.layoutparams (fill_parent, fill_parent));

The Layoutresource is a resource ID that can be found by flipping through the code.

2, a Phonewiondow has an object called Framelayout Mcontentparent, which is obtained from the layout XML in the Layoutresource above, This will also be the object in which the new view will be added to all users in the activity.

As a general description: An activity contains a Phonewiondow object, and all UI parts are placed in Phonewiondow.

Viewroot This class plays an intermediary role in the Android UI structure, connecting Phonewindow with Windowmanagerservice.

Windowmanagerservice we know it reads all the events in the Android system, keyboard events, trackball events and so on, how does it distribute to each activity? is through this viewroot.

At the time of each Phonewindow creation, the system adds Phonewindow Decorview object to a saved view array in Windowmanger, Windowmanger the View object while saving it, A new Viewroot object is also created to communicate the Windowmanagerservice.

You can see the code in Windowmanagerimpl and Viewroot, and we can see a W type in Viewroot, which derives from iwindow.stub to know that this can be used as a remote call.

res = swindowsession. Add (Mwindow, Mwindowattributes,

Gethostvisibility (), mattachinfo.mcontentinsets);

Iwindowsession is the remote invocation interface in Windowmanagerservice, which also shows that each Phonewindow created creates a viewroot, and will be registered in Windowmanagerservice.

Next, simply look at the delivery of the keyevent. In the Windowmanagerservice

Focus.mClient.dispatchKey (event);

The mclient of this piece of code is the W type in the Viewroot, and by remote invocation, you can see the processing of the Viewroot:

boolean handled = mView! = null

? Mview.dispatchkeyeventpreime (Event): true;

Mview is the Decorview object of Phonewindow, then how to pass the concrete can slowly go with

Approximate relationship between phonewindow,viewroot,activity

Related Article

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.