Relationship Between Activity, Window, and View in Android, androidactivity

Source: Internet
Author: User

Relationship Between Activity, Window, and View in Android, androidactivity

 

I. Overview

Activity can be said to be the carrier of the application (or it can be understood as the carrier of the interface, but not the interface), the user can draw the interface on it (the Activity itself does not draw the interface ), it also provides APIs for users to process events and maintain the life cycle of applications (Android applications are formed by accumulation of multiple activities, and each Activity has its own life cycle ).

Activity combines a Window (this is an abstract class, specifically PhoneWindow) object. When we expand an Activity, we call setContentView in the onCreate method. In fact, setContentView of the Window object is called. Therefore, interface painting is all implemented by the Window class (PhoneWindow class). Ii. Source Code Analysis1. Track the source code and enter the Activity to see the setContentView implementation 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 simply an object. So where is the Window object instantiated?

Let's take a look at the attach method in the Activity. Here we get a Window object.

mWindow = PolicyManager.makeNewWindow(this);


2. The setContentView method in the Activity shows that the interface painting is not completed by the Activity. It is implemented by calling setContentView of the Window class. So let's look at the Window class code:

public abstract void setContentView(int layoutResID);

We can see that the Window class is an abstract class and setContentView is an abstract method. Therefore, the specific implementation is completed by the class implementing the Window class (PhoneWindow ).

3. The document describes The only existing implementation of this abstract class is android. policy. PhoneWindow. Let's take a look at this class to know how The interface is drawn.

 1  @Override 2     public void addContentView(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. 8             Log.v(TAG, "addContentView does not support content transitions"); 9         }10         mContentParent.addView(view, params);11         final Callback cb = getCallback();12         if (cb != null && !isDestroyed()) {13             cb.onContentChanged();14         }15     }

First, determine whether mContentParent is null. If it is null, call the installDecor method (you can check the source code) and add the View.

There is a DecorView in the Window, which can be understood as "ViewRoot". The quotation mark means that this "ViewRoot" is actually a View or ViewGroup, which is the initial Root View. To build a view, add it here. So here we can see that the installDecor method is probably to build a Root View.

 

Iii. Summary

When an Activity is created, the system calls its attach method, adds it to ActivityThread, and creates a window object in the attach method.

We know that Window combines a DocerView. When you call setContentView, a View tree is still provided to DocerView. The View tree is an instance object that has been created, so we need to study what DocerView is and how it is created.

Let's look back at the setContentView method in the Window implementation. There is an installDecor method in the code, and this method has a generateDecor. GenerateDecor directly creates a new DecorView object.

protected DecorView generateDecor() {        return new DecorView(getContext(), -1); }


After the DocerView is created, mContentParent. addView (view, params); is called to add the view to the DocerView.

Summary:

  

The Activity calls the attach method in onCreate. The window object is created in the attach method. The DocerView object is not created when the window object is created. The user calls setContentView in the Activity and then calls setContentView of window. Then, the user checks whether DecorView exists. If not, the user creates a DecorView object and adds the user's View to DecorView.


PretendingStatementA: This blog article if 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

 

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.