Android Window, PhoneWindow, WindowManager, and Activity

Source: Internet
Author: User

Android Window, PhoneWindow, WindowManager, and Activity
Android Window, PhoneWindow, WindowManager, and Activity
To read this article, you must first understand the Context and the exact meaning of the Window. Next, we start with Activity. (The focus of this article is not to brief the startup process and learning plan of the Android app Activity ). We need to know that the Activity can be started explicitly or implicitly, but no matter what the startup method is. We need to know the ActivityManagerService service process at the application framework layer. Directory (source code directory/frameworks/base/services/java/com/android/server/am/ActivityManagerService. in the future, I will use the Android = source code directory (Android4.4 by default). In the Android Application Framework layer, ActivityManagerService is a very important interface, which not only starts Activity and Service, also responsible for Activity and Service management.

The ActivityManagerService In the Android Application Framework layer starts the Activity process as follows:

The specific startup process will not be described too much. Here we only need to know that the ActivityManagerService is used to forward the Activity Startup operation to ActivityThread. ActivityThread imports the corresponding Activity class through ClassLoader and then starts it.

Directory (Android4.4/frameworks/base/core/java/android/app/ActivityThread. java)

Next, we will go to ActivityThread to execute handleLaunchActivity.

 

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent){.........Activity a = performLaunchActivity(r, customIntent);if (a != null) {......handleResumeActivity(r.token, false, r.isForward,!r.activity.mFinished && !r.startsNotResumed);.......}.....}

In the receivmlaunchactivity

 

Public final class ActivityThread {...... private final Activity launch mlaunchactivity (ActivityClientRecord r, Intent customIntent) {ActivityInfo aInfo = r. activityInfo; if (r. packageInfo = null) {r. packageInfo = getPackageInfo (aInfo. applicationInfo, Context. CONTEXT_INCLUDE_CODE);} ComponentName component = r. intent. getComponent (); if (component = null) {component = r. intent. resolveActivity (mInitialAp Plication. getPackageManager (); r. intent. setComponent (component);} if (r.activityInfo.tar getActivity! = Null) {component = new ComponentName(r.activityInfo.packageName,r.activityInfo.tar getActivity);} // The Package and Component information of the Activity is collected. Activity activity = null; try {java. lang. classLoader cl = r. packageInfo. getClassLoader (); activity = mInstrumentation. newActivity (cl, component. getClassName (), r. intent); r. intent. setExtrasClassLoader (cl); if (r. state! = Null) {r. state. setClassLoader (cl) ;}} catch (Exception e ){......} // use ClassLoade to load your Activity according to the collected information. try {Application app = r. packageInfo. makeApplication (false, mInstrumentation); // according to AndroidManifest. create the Application tag in the xml configuration file ...... if (activity! = Null) {ContextImpl appContext = new ContextImpl (); appContext. init (r. packageInfo, r. token, this); appContext. setOuterContext (activity); CharSequence title = r. activityInfo. loadLabel (appContext. getPackageManager (); Configuration config = new Configuration (mConfiguration );...... activity. attach (appContext, this, getInstrumentation (), r. token, r. ident, app, r. intent, r. activityInfo, title, r. parent, r. emb EddedID, r. lastNonConfigurationInstance, r. lastNonConfigurationChildInstances, config); // create if (customIntent! = Null) {activity. mIntent = customIntent;} r. lastNonConfigurationInstance = null; r. lastNonConfigurationChildInstances = null; activity. mStartedActivity = false; int theme = r. activityInfo. getThemeResource (); if (theme! = 0) {activity. setTheme (theme);} activity. mCalled = false; mInstrumentation. callActivityOnCreate (activity, r. state );...... r. activity = activity; r. stopped = true; if (! R. activity. mFinished) {activity. initialize mstart (); r. stopped = false;} if (! R. activity. mFinished) {if (r. state! = Null) {mInstrumentation. callActivityOnRestoreInstanceState (activity, r. state) ;}} if (! R. activity. mFinished) {activity. mCalled = false; mInstrumentation. callActivityOnPostCreate (activity, r. state); if (! Activity. mCalled) {throw new SuperNotCalledException ("Activity" + r. intent. getComponent (). toShortString () + "did not call through to super. onPostCreate () ") ;}}} r. paused = true; mActivities. put (r. token, r);} catch (SuperNotCalledException e ){......} catch (Exception e ){......} return activity ;}......}

For more information about ActivityThread activation, see @ Android app startup source code analysis.
Next, we track the attach directory of the Activity (Android 4.4/frameworks/base/core/java/android/app/Activity. java)
Final void attach (Context context, ActivityThread aThread, Instrumentation instr, IBinder token, Application application, Intent intent, ActivityInfo, CharSequence title, Application application, Intent intent, ActivityInfo info, CharSequence title, configuration config) {attach (context, aThread, instr, token, 0, application, intent, info, title, parent, id, lastNonConfigurationInstances, config);} f Inal void attach (Context context, ActivityThread aThread, Instrumentation instr, IBinder token, int ident, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, NonConfigurationInstances failed, configuration config ){..... mWindow = PolicyManager. makeNewWindow (this); mWindow. setCallback (this );...... // initialize the basic information ....... mWindow. setWin DowManager (WindowManager) context. getSystemService (Context. WINDOW_SERVICE), mToken, mComponent. flattenToString (), (info. flags & ActivityInfo. FLAG_HARDWARE_ACCELERATED )! = 0); if (mParent! = Null) {mWindow. setContainer (mParent. getWindow ();} mWindowManager = mWindow. getWindowManager ();......}
Here we finally see our Window. We know that in Android, every Activity component has an associated Window object to describe an application Window. Each application window contains a View object to describe the View of the application window. The application window view is actually used to implement the UI content and layout, that is, the UI content and layout of each Activity component are implemented through a View object in the Window object associated with it (Android 4.4/frameworks/base/core/java/com /android/internal/policy/PolicyManager. java)
public final class PolicyManager {private static final String POLICY_IMPL_CLASS_NAME ="com.android.internal.policy.impl.Policy";private static final IPolicy sPolicy;static {try{Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);sPolicy = (IPolicy)policyClass.newInstance();}catch(....){.......}}public static Window makeNewWindow(Context context) {return sPolicy.makeNewWindow(context);}public static LayoutInflater makeNewLayoutInflater(Context context) {return sPolicy.makeNewLayoutInflater(context);}public static WindowManagerPolicy makeNewWindowManager() {return sPolicy.makeNewWindowManager();}}

Directory in the corresponding Policy (Android 4.4/frameworks/base/policy/src/com/android/internal/policy/impl/Policy. java)
public class Policy implements IPolicy {private static final String[] preload_classes = {"com.android.internal.policy.impl.PhoneLayoutInflater","com.android.internal.policy.impl.PhoneWindow",.....};static {for (String s : preload_classes) {try{Class.forName(s);}catch (ClassNotFoundException ex) {}}public Window makeNewWindow(Context context) {return new PhoneWindow(context);}public LayoutInflater makeNewLayoutInflater(Context context) {return new PhoneLayoutInflater(context);}public WindowManagerPolicy makeNewWindowManager() {return new PhoneWindowManager();}}

Here we start to create a PhoneWindow that inherits the Window. Here, if we want to continue the analysis, we must know
1. PhoneWindow implements Window 2,
The actual type of view objects contained in the application window is DecorView. The DecorView class inherits the View class and is used as a container (ViewGroup ).






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.