The relationship between activity, Window, and view in Android

Source: Internet
Author: User

I. Overview

Activity can be said to be the carrier of the application (can also be understood as the interface of the carrier, but not the interface), the user can draw the interface (activity itself does not draw the interface), and provide users with the API to handle events, maintain the life cycle of the application (Android applications are made up of multiple Activity accumulates, and each activity has its own life cycle).

the activity internally combines a window (this is an abstract class, specifically a Phonewindow) object. We wrote it ourselves. When an activity is extended, calling Setcontentview in the OnCreate method is actually called the Setcontentview of the Window object, So, the interface drawing is all done by the implementation class (Phonewindow Class) of the window class.       Second, source code analysis  1. Trace the source into the Activity to see the implementation of Setcontentview as follows:
1   Public void setcontentview (int  layoutresid) {2        GetWindow (). Setcontentview ( LAYOUTRESID); 3         Initactionbar (); 4     }

Look at the second line of code and get a Window object first.

1  Public Window GetWindow () {2         return Mwindow; 3     }

GetWindow is simple just to return an object, so where exactly is the window object instantiated?

We can look at the Attach method in Activity, which gets a Window object

Mwindow = Policymanager.makenewwindow (this);


2. The Setcontentview method in the activity can be seen, the interface drawing is not done by the activity, he is called the Window class Setcontentview to implement. So let's take a look at the code for the Window class:

 Public Abstract void setcontentview (int layoutresid);

As we can see, the Window class is an abstract class, and Setcontentview is an abstract method. Therefore, its implementation is accomplished by implementing the class of the window Class (Phonewindow).

3. Already described in the documentation: the only existing implementation of this abstract class is Android.policy.PhoneWindow. We're going to look at this class to see how the interface finishes drawing.

1 @Override2      Public voidAddcontentview (view view, Viewgroup.layoutparams params) {3         if(Mcontentparent = =NULL) {4 Installdecor ();5         }6         if(Hasfeature (feature_content_transitions)) {7             //TODO Augment The scenes/transitions API to support this.8LOG.V (TAG, "Addcontentview does not support content transitions");9         }Ten Mcontentparent.addview (view, params); One         FinalCallback cb =Getcallback (); A         if(CB! =NULL&&!isdestroyed ()) { - cb.oncontentchanged (); -         } the}

First determine whether mcontentparent is empty, if it is empty call the Installdecor method (you can look at the source code), and finally add a View.

There is a decorview in the window, which can be understood as "viewroot", the quotation marks are actually the "viewroot" is a view or ViewGroup, is the most initial root views. Building a view is adding to this. So here we can probably see that the Installdecor method is probably to build a root view.

Iii. Summary

When activity is created, the system calls its attach method, adds it to the activitythread, and creates a Window object in the Attach method.

We know that window combines a docerview, and when the user calls Setcontentview, it still gives Docerview a View tree. The view tree is a well-created instance object, so we're going to look at what Docerview is and how it was created.

We look back at the Setcontentview method inside the window implementation, there is a Installdecor method in the code, there is a generatedecor in this method. Generatedecor directly new a Decorview object

protected Decorview Generatedecor () {        returnnew Decorview (GetContext (),-1);}


When Docerview is created, it is called mcontentparent.addview (view, params); To add the view to the Docerview.

Finally, summarize:

  

The activity calls the Attach method when OnCreate, and the Window object is created in the Attach method. The Window object was created without creating a Docerview object. The user invokes Setcontentview in the activity and then calls the window's Setcontentview, which checks to see if Decorview exists, creates a Decorview object if it does not exist, and then adds the user's own View to the The Decorview.


Pretend to declare : This Bo article is not special note are original, if you need to reprint please keep the original link (http://www.cnblogs.com/kest/p/5141817.html) and author information k_est

The relationship between activity, Window, and view in Android

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.