Windows creation process for Android

Source: Internet
Author: User

I want to add!

A window is essentially a view, and the window class is just an abstraction of an application window.

① the code essence of initiating activity is a process of creating activity, which is done by Activitythread. The code is as follows:

2109 Activity activity =NULL;2110Try {2111 Java.lang.ClassLoader cl =R.packageinfo.getclassloader ();2112 activity =minstrumentation.newactivity (2113cl, Component.getclassname (), r.intent);2114Strictmode.incrementexpectedactivitycount (Activity.getclass ());2115R.intent.setextrasclassloader (CL);2116if(R.state! =NULL) {2117R.state.setclassloader (CL);2118            }2119}Catch(Exception e) {2120if(!minstrumentation.onexception (activity, E)) {2121Throw NewRuntimeException (2122 "Unable to instantiate activity" +Component2123 + ":" +e.tostring (), e);2124            }2125}

Use ClassLoader to load the specified activity's class file from the program file.

After ② constructs the specified activity object, it then calls the activity's attach method, as follows:

2144 this                , Getinstrumentation (), R.token,2145                        r.ident, app, R.intent, R.activityinfo, Title, R.parent,2146                        r.embeddedid, r.lastnonconfigurationinstances, config);

The first function of the Attach method is to set internal variables for the newly constructed activity, not the focus of the present, ignoring. The second function is to create a Window object for the activity, with the following code:

5180Final voidAttach (context context, Activitythread Athread,5181 instrumentation instr, IBinder token,intident,5182application Application, Intent Intent, activityinfo info,5183charsequence title, Activity parent, String ID,5184nonconfigurationinstances lastnonconfigurationinstances,5185Configuration config) {5186Attachbasecontext (context);51875188 mfragments.attachactivity ( This, Mcontainer,NULL);5189 5190 Mwindow = Policymanager.makenewwindow ( This);5191 Mwindow.setcallback ( This);

The Window object is completed by Policymanager's static method, Makenewwindow (). Policymanager creates a different product type window depending on the type (you can look at the policy design pattern), where the code simply creates a Phonewindow object. There are only two specific implementations of window in the current framework: phonewindow-mobile phone, midwindow-portable Internet device, and here you can see the original purpose of Android design. After the Window object is created, assign its value to the activity's internal variable Mwindow and set the window's callback callback interface to the current Activity object (obviously the activity needs to implement this interface). This is why user messages can be delivered to the activity.

③ you create a Window object, you need to assign a value to the Mwindowmanager of the Window object. This is not the point of this article, but it still shows that the assignment code is as follows:

Mwindow.setwindowmanager (                windowmanager) Context.getsystemservice (context.window_service),                MToken, Mcomponent.flattentostring (),                & activityinfo.flag_hardware_accelerated)! = 0); if NULL ) {     = Mwindow.getwindowmanager ();

Window's Setwindowmanager method:

Mwindowmanager = ((Windowmanagerimpl) WM). Createlocalwindowmanager (this);

Implementation of the Createlocalwindowmanager method:

 Public Windowmanagerimpl Createpresentationwindowmanager (display display) {      returnnew  Windowmanagerimpl (display, Mparentwindow);}

So in the end, window is creating a Windowmanagerimpl object, and WindowManager is an interface.

After the ④ is configured with Activity and window, the next step is to add a real display element view or ViewGroup to the window. Activitythead a series of processes that initiate activity, all in its performlaunchactivity (). The Callactivityoncreate () method is executed after attach (), which triggers the OnCreate method of the activity. And in the OnCreate method, we will Setcontentview () and look at its code:

 Public void setcontentview (int  layoutresid) {     GetWindow (). Setcontentview (LAYOUTRESID);     Initactionbar ();}

This method invokes the Setcontentview method of the Window object, and the next step is to look at how the window Layout.xml as the display element of the window interface.

Phonewindow's Setcontentview () method is as follows:

284 Public voidSetcontentview (intLayoutresid) {285if(Mcontentparent = =NULL) {286Installdecor ();287}Else {288mcontentparent.removeallviews ();289         }290mlayoutinflater.inflate (Layoutresid, mcontentparent);291FinalCallback cb =Getcallback ();292if(CB! =NULL&&!isdestroyed ()) {293cb.oncontentchanged ();294         }295}

The code first calls the Installdecor () method to install a window decoration for the Windows class, the so-called window decorations are common on the interface of the title bar, we write the Layout.xml will be included in the window decoration, called the window content. Window decorations and window content relationships such as:

Several window decorations are defined in the framework, and the Installdecor () code is as follows:

3075Private voidInstalldecor () {3076if(Mdecor = =NULL) {3077 Mdecor =Generatedecor ();3078mdecor.setdescendantfocusability (viewgroup.focus_after_descendants);3079 Mdecor.setisrootnamespace (true);3080if(!minvalidatepanelmenuposted && Minvalidatepanelmenufeatures! = 0) {3081mdecor.postonanimation (minvalidatepanelmenurunnable);3082            }3083        }3084if(Mcontentparent = =NULL) {3085 mcontentparent = Generatelayout (Mdecor);

The code has done three main tasks:

    1. Generatedecor () generates a Decorview object that is assigned to Mdecor,mdecor not a window decoration, but a unique child view of the window adornment.
    2. Selects a different window decoration based on the parameters specified by the user, and then modifies the window as a child window of the Mdecor, which is done in Generatelayout () called Mdecor.addview ().
    3. Assigns a value to the mcontentparent variable, generatelayout The origin of the return value:
...
ViewGroup contentparent = (viewgroup) Findviewbyid (id_android_content); return contentparent;

So what is this id_android_content?

public static final int id_android_content = com.android.internal.r.id.content;

is to find a id=content layout, why return this and return this to do what, please see below!

The second one is very important, and selecting the unused window decoration based on the user-specified parameters is actually the style of the specified activity.

How does the view that calls Mdecor.addview () in Generatelayout () come about? Look at the following code:

int Layoutresource;
...
int features = Getlocalfeatures (); // Here's why we wrote Requestfeature () in OnCreate.

2967if((Features & (1 << feature_left_icon) | (1 << feature_right_icon))) ! = 0) {2968if(misfloating) {2969 Typedvalue res =NewTypedvalue ();2970GetContext (). Gettheme (). Resolveattribute (2971 Com.android.internal.r.attr.dialogtitleiconsdecorlayout, Res,true);2972 Layoutresource =Res.resourceid;2973}Else {2974 Layoutresource =com.android.internal.r.layout.screen_title_icons;2975            }2976//XXX Remove This once action bar supports these features.2977removefeature (feature_action_bar);2978//System.out.println ("Title icons!");2979}Else if((Features & (1 << feature_progress) | (1 << feature_indeterminate_progress))) ! = 02980 && (features & (1 << feature_action_bar)) = = 0) {2983 Layoutresource =com.android.internal.r.layout.screen_progress;2985}Else if((Features & (1 << feature_custom_title))! = 0) {2988if(misfloating) {2989 Typedvalue res =NewTypedvalue ();2990GetContext (). Gettheme (). Resolveattribute (2991 Com.android.internal.r.attr.dialogcustomtitledecorlayout, Res,true);2992 Layoutresource =Res.resourceid;2993}Else {2994 Layoutresource =Com.android.internal.r.layout.screen_custom_title;2995            }2997removefeature (feature_action_bar);2998}Else if((Features & (1 << feature_no_title)) = = 0) {3001if(misfloating) {3002 Typedvalue res =NewTypedvalue ();3003GetContext (). Gettheme (). Resolveattribute (3004 Com.android.internal.r.attr.dialogtitledecorlayout, Res,true);3005 Layoutresource =Res.resourceid;3006}Else if((Features & (1 << feature_action_bar))! = 0) {3007 Layoutresource =Com.android.internal.r.layout.screen_action_bar;3008}Else {3009 Layoutresource =Com.android.internal.r.layout.screen_title;3010            }3012}Else if((Features & (1 << feature_action_mode_overlay))! = 0) {3013 Layoutresource =Com.android.internal.r.layout.screen_simple_overlay_action_mode;3014}Else {3016 Layoutresource =com.android.internal.r.layout.screen_simple;3018}

The code above is to load different layouts according to the different decorations:

3022        null); 3023        New  viewgroup.layoutparams (match_parent, match_parent)); 30243025        ViewGroup contentparent = (viewgroup) Findviewbyid (id_android_content);

After the layout is loaded, add the layout to DECORVEIW and return the layout of the id=content in the layout.

R.layout.screen_simple's Code:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:fitssystemwindows= "true"android:orientation= "vertical">     <viewstubAndroid:id= "@+id/action_mode_bar_stub"Android:inflatedid= "@+id/action_mode_bar"Android:layout= "@layout/action_mode_bar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />     <FramelayoutAndroid:id= "@android: Id/content"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:foregroundinsidepadding= "false"android:foregroundgravity= "Fill_horizontal|top"Android:foreground= "? Android:attr/windowcontentoverlay" /> </LinearLayout> 

So, back to the Setcontentview method, the layout of the id=content is returned, and as we write the Layout.xml parent layout, the window's view settings are complete.

Windows creation process for 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.