2013年8月13日星期二

android in the window, view, activity specific relationship

 

by discussing this issue, we are able to google is an insight into the understanding of object-oriented model, able to understand some of the underlying android call. This is a very common interview questions.

 

this article we would like to address four questions:

 
      
  • Android in view of the display view it?
  •   
  • Activity, window, View What is the relationship?
  •   
  • LayOutInflater fill what is?
  •   
  • LayOutInflater specifically how to do?
  •  
 

First, from activity to begin with, we have to know setcontentview talking about activity and attach methods. setcontentview in, mainly used to fill the appropriate layout file. As for attach method, this method rarely used, but it is very important.

 

we track java source code, we clearly see that this activity is actually called phonewindow of setcontentview in approach to the presentation of the interface. His class diagram is as follows:

 

 

while phonewindow have initialized a ViewGroup object, this ViewGroup subclass, you can display each control's view, there is a LayoutInflator the xml file to populate the corresponding view. Their class diagram is as follows:

 

 

we look at the specific source code is as follows:

 

First, we found activity in the attach method, this is the program starts when the most important method. , Which is the source code:

 
  
final void attach(Context context, ActivityThread aThread, 
Instrumentation instr, IBinder token,
int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
Object lastNonConfigurationInstance,
HashMap
<String,Object> lastNonConfigurationChildInstances,
Configuration config) {
attachBaseContext(context);

mWindow
= PolicyManager.makeNewWindow(this);
mWindow.setCallback(
this);
if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
mWindow.setSoftInputMode(info.softInputMode);
}
mUiThread
= Thread.currentThread();

mMainThread
= aThread;
mInstrumentation
= instr;
mToken
= token;
mIdent
= ident;
mApplication
= application;
mIntent
= intent;
mComponent
= intent.getComponent();
mActivityInfo
= info;
mTitle
= title;
mParent
= parent;
mEmbeddedID
= id;
mLastNonConfigurationInstance
= lastNonConfigurationInstance;
mLastNonConfigurationChildInstances
= lastNonConfigurationChildInstances;

mWindow.setWindowManager(
null, mToken, mComponent.flattenToString());
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager
= mWindow.getWindowManager();
mCurrentConfig
= config;
}
 
 

we see policymanager object has a window object, the window is a window of reality. He is an abstract class, but also how new out of this object, we continue to focus on. This came policymanager a class. Look makenewwindow this method:

 
  
public static Window makeNewWindow(Context context) { 
return sPolicy.makeNewWindow(context);
}
 
 

he is passing context calls ipolicy interfaces makeNewWindow method to return a window object. Continue to root out, we look makenewwindow this method the real true colors.

 

This is ipolicy makewindow itself an abstract way, he was awarded to the policy implementation, this policy is a phonewindow new object. This is how the class phonewindow like it. Concerned about the source code we learned that he was with the window inherit this base class, spare a large circle, found this makenewwindow really new out a window object. He constructor looks like this:

 
  
    public PhoneWindow(Context context) { 
super(context);
mLayoutInflater
= LayoutInflater.from(context);
}
 
 

found him with a layoutflater layout file is populated from a corresponding interface, through the source code of this great article proof, we see that window, activity, and view the relationship is like this:
activity in new out of a window, while the window through this phonewindow subclass to get a sub-class. This is a typical composite mode. A classic application, we hope to bring help.

 

add a window on how, through setcontentview way, we see that this is still the setcontentview way through phonewindow his source code is as follows:

 
  
    public void setContentView(View view, ViewGroup.LayoutParams params) { 
if (mContentParent == null) {
installDecor();
}
else {
mContentParent.removeAllViews();
}
mContentParent.addView(view, params);
final Callback cb = getCallback();
if (cb != null) {
cb.onContentChanged();
}
}
 
 

he is through mcontentparent to add a view, which further reveals the relationship between the window and the view.

 

That is my understanding.

 

learn every day.

 

 

 

 

没有评论:

发表评论