In fact, what is actually shown to the user in Android is that Windows and view,activity in Android are primarily dealing with logic issues such as lifecycle management, building windows, and so on. In Android , window management is a more important piece because he is directly responsible for presenting the content to the user and interacting with the user. Respond to user input and so on.
In the management of the window, it is necessary to first say Viewmanager this interface, this interface mainly has the following implementation sub-interface and implementation class, respectively: WindowManager and ViewGroup inside there are three important methods:
* AddView ();
* Updateviewlayout ();
* Removeview ();
In WindowManager, the AddView method represents a top-level view (that is, Decorview) in the main window that is added to WindowManager and a session is established. This is explained in more detail next. Let's look at window first.
Window:
Windows is a window in Android that represents the top-level window meaning, that is, the main window, which has two implementation classes,Phonewindow and Midwindow, our general Activity corresponds to the main Phonewindow, in the activity is often used in the Setcontentview and other methods are also implemented in this.
@Override
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 (); Update when window class capacity changes
}
}
Each main window has a view called Decorview, which is the top view in the main window (which is actually viewgroup), with two member variables in view called Mparent, Mchildren, which are used to manage the hierarchy of the view. And ViewGroup is the management of a group of view. As a result, all view networks are established in the ViewGroup. The final viewgroup is attached to the main window. This makes it easy to find a specific view through the Findviewbyid in the window. Event handling in view is also handled according to this path.
Let's look at two important methods in Activitythead (as far as Activitythead will be described in detail in one article):
Performlaunchactivity ();
Handleresumeactivity ();
In Performlaunchactivity, the Activity.attach method is called to create a window, when the activity is started in the Handleresumeactivity method, The main window will be added to the WindowManager
View decor =r.window.getdecorview (); Get the top view of the window
Decor.setvisibility (view.invisible);
Viewmanager wm= A.getwindowmanager (); WindowManager inherited from Viewmanager
Windowmanager.layoutparams l =r.window.getattributes ();
A.mdecor = Decor;
L.type =windowmanager.layoutparams.type_base_application;
L.softinputmode |= forwardbit;
if (a.mvisiblefromclient) {
A.mwindowadded = true;
Wm.addview (decor, L); is actually adding the top view of the main window to the Windowmangaer
}
Let's look at WindowManager again.
WindowManager:
WindowManager is primarily used to manage some of the window's status, properties, view additions, deletions, updates, window order, message collection, and processing.
An instance of WindowManager can be obtained by Context.getsystemservice (context.window_service) .
WindowManager inherits from the Viewmanager, which involves three important methods of window management, namely:
* AddView ();
* Updateviewlayout ();
* Removeview ();
There is also an important static class Layoutparams in WindowManager . It allows you to set and get some properties of the current window.
Let's take a look at the AddView () method, and in AddView, we'll use Layoutparams to get the View property of window and create a viewroot for each window. Viewroot is the bridge between the view and the WindowManager, and the real way to pass the view to WindowManager is through the Viewroot Setview () method, The Viewroot implements message passing between view and WindowManager. When you add the main window to Windowmanger, it first establishes a proxy object:
Wm= (Windowmanagerimpl) context.getsystemservice (Context.window_service)
and open the session (Iwindowsession), and then Window will connect to WindowManager through the session ,
Take a look at the Setview method:
try {
Res =swindowsession.add (Mwindow, Mwindowattributes,
Gethostvisibility (), mattachinfo.mcontentinsets);
} catch (RemoteException e) {
madded = false;
MView = null;
Mattachinfo.mrootview =null;
Unscheduletraversals ();
Throw newruntimeexception ("Adding window Failed", e);
} finally {
if (restore) {
Attrs.restore ();
}
}
In this code, Viewroot adds the window to the WindowManager through Iwindowsession. Viewroot inherited the handler, in fact, its essence is a handler, window view of the event processing, message sending, callback, etc. will be handled through Viewroot.
This completes the window to be added to the WindowManager, and by the WindowManager to manage the window view, events, message collection processing and so on.
http://blog.csdn.net/xieqibao/article/details/6567814