Android Kernel Anatomy-----Activity Initiation Process

Source: Internet
Author: User

Main class: Activitythread (An application's start-up portal) Activityservicemanager (responsible for all activity management)

Each app's startup generates a Activitythread thread responsible for maintaining the app

When Activitythread receives information from the initiating activity sent by Activityservicemanager

Call Schedulelaunchactivity

Public final void Schedulelaunchactivity (Intent Intent, ibinder tokens, int ident,                activityinfo info, Bundle State, Lis T<resultinfo> pendingresults,                list<intent> pendingnewintents, Boolean notresumed, Boolean IsForward) {            Activityclientrecord r = new Activityclientrecord ();            R.token = token;            R.ident = ident;            R.intent = Intent;            R.activityinfo = info;            R.state = State;            R.pendingresults = Pendingresults;            r.pendingintents = pendingnewintents;            r.startsnotresumed = notresumed;            R.isforward = Isforward;            Queueorsendmessage (h.launch_activity, R);        }
It then sends an asynchronous processing request through handler processing the request in Handlemessage

public void Handlemessage (Message msg) {            if (debug_messages) slog.v (TAG, ">>> handling:" + msg.what);            Switch (msg.what) {case                launch_activity: {                    Activityclientrecord r = (activityclientrecord) msg.obj;                    R.packageinfo = Getpackageinfonocheck (                            r.activityinfo.applicationinfo);                    Handlelaunchactivity (R, NULL);                } Break, ...}
Handlelaunchactivity:

 Private final void Handlelaunchactivity (Activityclientrecord R, Intent customintent) {//If we are getting ready        To GC after going to the background, well//We is back active so skip it.        Unschedulegcidler ();        if (LOCALLOGV) slog.v (TAG, "Handling launch of" + R);        Activity a = performlaunchactivity (R, Customintent);            if (A! = null) {r.createdconfig = new Configuration (mconfiguration);            Bundle oldstate = r.state;            Handleresumeactivity (R.token, False, R.isforward); if (!r.activity.mfinished && r.startsnotresumed) {//The Activity manager actually wants this one  To-start out//paused, because it needs-to is visible but not ' t in the//foreground.                We accomplish this by going through the//normal startup (because activities expect to go through       Onresume () The first time they run, before their window         is displayed), and then pausing it. However, in this case//We do-not-need to does the full pause cycle (of freezing//and suc                h) Because the activity manager assumes it can just//retain the current state it has.                    try {r.activity.mcalled = false;                    Minstrumentation.callactivityonpause (r.activity);                    We need to keep around the original state, in case//We need to be created again.                    R.state = oldstate; if (!r.activity.mcalled) {throw new Supernotcalledexception ("Activity                    "+ r.intent.getcomponent (). toshortstring () +" didn't "call through to Super.onpause ()");                }} catch (Supernotcalledexception e) {throw e; } catch (Exception e) {if (!miNstrumentation.onexception (R.activity, E)) {throw new RuntimeException (                                "Unable to pause activity" + r.intent.getcomponent (). Toshortstring ()                    + ":" + e.tostring (), E);            }} r.paused = true;             }} else {//If there was a error, for any reason, tell the activity//manager to stop us. try {activitymanagernative.getdefault (). Finishactivity (R.token, ACTIVITY.R            esult_canceled, NULL); } catch (RemoteException ex) {}}}
The activity object was finally obtained by reflection through Performlaunchactivity.

 Private final Activity performlaunchactivity (Activityclientrecord R, Intent customintent) {//System.out.println (        "##### [" + system.currenttimemillis () + "] activitythread.performlaunchactivity (" + R + ")");        Activityinfo ainfo = R.activityinfo; if (R.packageinfo = = null) {R.packageinfo = Getpackageinfo (Ainfo.applicationinfo, context.c        Ontext_include_code);        } componentname component = R.intent.getcomponent (); if (component = = NULL) {component = R.intent.resolveactivity (Minitialapplication.getpackageman            Ager ());        R.intent.setcomponent (component);                     } if (r.activityinfo.targetactivity! = null) {component = new ComponentName (R.activityinfo.packagename,        r.activityinfo.targetactivity);        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) {if (!minstrumentation.onexception (activity, E)) {throw new Run Timeexception ("Unable to instantiate activity" + Component + ":" + e.tostring (),            e);            }} try {Application app = R.packageinfo.makeapplication (false, minstrumentation);            if (LOCALLOGV) slog.v (TAG, "performing launch of" + R);                     if (LOCALLOGV) slog.v (TAG, R + ": app=" + App + ", appname=" + app.getpackagename () + ", pkg=" + r.packageinfo.getpackagename () + ", comp=" + r.intent.getcomponent (). t            Oshortstring () + ", dir=" + r.packageinfo.getappdir ()); 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); if (debug_configuration) slog.v (TAG, "launching activity" + R.activityinfo.name + "with config"                + config); Activity.attach (AppContext, this, getinstrumentation (), R.token, R.ident, app, R.intent, r.activity Info, title, R.parent, R.embeddedid, R.lastnonconfigurationinstance, R.LASTN                onconfigurationchildinstances, config);                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); if (!activity.mcalled) {throw new Supernotcalledexception ("activity" + r.inte                Nt.getcomponent (). toshortstring () + "did not call through to Super.oncreate ()");                } r.activity = activity;                R.stopped = true;                    if (!r.activity.mfinished) {Activity.performstart ();                r.stopped = false; } if (!r.activity.mfinished) {if (r.state! = null) {Minstrument Ation.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) {throw e; } catch (Exception e) {if (!minstrumentation.onexception (activity, E)) {throw new Runtimeexcep            tion ("Unable to start activity" + Component + ":" + e.tostring (), E);    }} return activity; }
Executed in the performlaunchactivity.

Activity.attach (AppContext, this, getinstrumentation (), R.token,----attach additional instructions to save the Attach object in the Contextimpl () method
R.ident, App, R.intent, R.activityinfo, title, R.parent,
R.embeddedid, R.lastnonconfigurationinstance,
r.lastnonconfigurationchildinstances, config);

Minstrumentation.callactivityoncreate (activity, r.state); ----OnCreate minstrumentation holds activity objects inside

Activity.performstart ();----OnStart

if (!r.activity.mfinished) {                    if (r.state! = null) {                        minstrumentation.callactivityonrestoreinstancestate ( activity, r.state);                    }                }
After onpause to restore the status of the


Android Kernel Anatomy-----Activity Initiation Process

Related Article

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.