Android layout file loading process analysis: Activity.setcontentview () Source Code Analysis

Source: Internet
Author: User

Everyone knows that calling the Activity.setcontent () method in the activity's OnCreate () can load the layout file to set the activity's display interface. This article will start with the source code of Setcontentview () and analyze the call chain involved in the layout file loading. The source code used in this article is android-19.

Step 1, Activity.setcontentview (INTRESID)

public void Setcontentview (int layoutresid) {GetWindow (). Setcontentview (Layoutresid); Initactionbar ();} Public Window GetWindow () {return mwindow;}
The method invokes the activity member's Mwindow,mwindow as a Window object. The Windown object is an abstract class that provides a display policy and behavior policy for the standard UI. In the SDK, only the Phonewindow class implements the window class, and Setcontentview () in window is an empty function, so the last call is the method of the Phonewindow object.

Step 2, Phonewindow.setcontentview ()

@Overridepublic void Setcontentview (int layoutresid) {if (mcontentparent = = null) {Installdecor ();} else {mcontentparent . Removeallviews ();} Add the layout file to Mcontentparent mlayoutinflater.inflate (Layoutresid, mcontentparent); final Callback cb = Getcallback (); if ( CB! = null &&!isdestroyed ()) {cb.oncontentchanged ();}}
The method first sets the mcontentparent according to whether the mcontentparent is empty. Mcontentparent is the ViewGroup type, and if it has already been initialized, remove all child view, otherwise call Installdecor () to initialize. The resource file is then converted to the view tree and added to the Mcontentparent view.

Step 3, Phonewindow.installdecor ()
This code is long, the following pseudo-code only introduces logic, readers can view the source code themselves.

private void Installdecor () {if (Mdecor = = null) {/* Creates a Decorview object and sets the corresponding property. Decorview is the root of all view View*/mdecor = Generatedecor (); Mdecor.setdescendantfocusability (Viewgroup.focus_after_ descendants); Mdecor.setisrootnamespace (true); if (!minvalidatepanelmenuposted && Minvalidatepanelmenufeatures! = 0) {mdecor.postonanimation (minvalidatepanelmenurunnable);}} if (mcontentparent = = null) {/* Create mcontentparent object */mcontentparent = Generatelayout (Mdecor);//Set up decor part of UI to I Gnore fitssystemwindows if Appropriate.mDecor.makeOptionalFitsSystemWindows (); Mtitleview = (TextView) Findviewbyid ( Com.android.internal.r.id.title); if (Mtitleview! = null) {Mtitleview.setlayoutdirection (Mdecor.getlayoutdirection ( );//Based on the features value, set the relevant property of the TITLE if ((Getlocalfeatures () & (1 << feature_no_title))! = 0) {View Titlecontainer = fi Ndviewbyid (Com.android.internal.r.id.title_container); if (titlecontainer! = null) {titlecontainer.setvisibility ( View.gone);} else {mtitleview.setvisibility (view.gone);} IF (mcontentparent instanceof Framelayout) {((framelayout) mcontentparent). Setforeground (null);}} else {mtitleview.settext (mtitle);}} else {//If there is no title, set Actionbar related properties, such as callback function, style attribute Mactionbar = (actionbarview) Findviewbyid ( Com.android.internal.r.id.action_bar); if (Mactionbar! = null) {//.......//Deferred calling invalidate, placing oncreateoptionsmenu in OnCreate called Mdecor.post (New Runnable () {public void run () {//Invalidate If the Panel menu hasn ' t been created before th Is.  Panelfeaturestate st = Getpanelstate (Feature_options_panel, false); if (!isdestroyed () && (st = = NULL | | st.menu = = NULL) {Invalidatepanelmenu (Feature_action_bar);}}});}}}
The code to create the Mcontentparent object is as follows:

Protected ViewGroup generatelayout (Decorview decor) {//Apply data from current theme.//gets the property data for the currently window theme, The file location for the related field is: sdk\platforms\android-19\data\res\values\attrs.xml//These property values can be set activityandroid in Androidmanifest.xml: Theme= "" can also be set by//requestwindowfeature () in the activity's oncreate. Note that this method must be called before setcontentview typedarray a = Getwindowstyle (); Gets the theme//set in Android:theme= "" to set the characteristics and layout of Phonewindow based on the theme property values, including attributes such as title, ActionBar, ActionBar mode, window size, and so on. ...//inflate the window decor.//according to the window feature set above to determine the layout file//Android SDK built-in Layout folder location: sdk\platforms\android-19 \data\res\layout Typical window layout files are: r.layout.dialog_titile_icons r.layout.screen_title_icons r.layout.s         Creen_progress R.layout.dialog_custom_title R.layout.dialog_title R.layout.screen_title Most commonly used Activity window decorates layout file r.layout.screen_simple//Full-screen Activity window layout file int layoutresource;int features = Getlocalfea Tures (); Will call Requesetwindowfeature ()//... mdecor.startchanging ();//Will layoutThe file is converted to the view number and then added to Decorview in the view in = Mlayoutinflater.inflate (Layoutresource, NULL);d the Ecor.addview (in, new Viewgroup.layoutparams (Match_parent, match_parent));//Take out the ViewGroup as content, android:id= "@android: Id/content", is a framelayoutviewgroup contentparent = (viewgroup) Findviewbyid (id_android_content); if (contentparent = = null) {throw New RuntimeException ("window couldn ' t find Content container View");} if ((Features & (1 << feature_indeterminate_progress)) = 0) {ProgressBar PROGRESS = Getcircularprogressbar (fal SE); if (progress! = null) {Progress.setindeterminate (true);}} Set the background, title, frame, and other properties of the top window to the previous if (getcontainer () = = null) {//...} Mdecor.finishchanging (); return contentparent;}
Summary: Setcontentview the process of loading a layout file into a program window can be summarized as:
1. Create a Decorview object that will act as the root view of the entire application window
2, according to the android:theme= "" or requestwindowfeature set the properties of the window, according to these properties select Load system built-in layout file
3. Remove the framelayout with the content ID from the loaded layout file as the parent of content
4. Load the layout file in Setcontentview into the framelayout removed in the 3


Finally, when AMS (Activitymanagerservice) prepares to resume an activity, it will callback the activity's Handleresumeactivity () method,
This method invokes the activity's Makevisible method, which shows the Mdecor view family we just created.

This method is called when the system resume an activity,  final void handleresumeactivity (IBinder token, Boolean clearhide, Boolean Isforward) {      Activityrecord r = performresumeactivity (token, clearhide);      //...       if (r.activity.mvisiblefromclient) {           r.activity.makevisible ();       }  
Makevisible is located in the Activitythread class with the following code:

void Makevisible () {      if (!mwindowadded) {          Viewmanager wm = Getwindowmanager ();   Gets the WindowManager object          wm.addview (Mdecor, GetWindow (). GetAttributes ());          mwindowadded = true;      }      Mdecor.setvisibility (view.visible); Put it in the display state  }  

Reference article: http://blog.csdn.net/qinjuning/article/details/7226787




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.