The main entrance of Android software is also main
, this main method is defined in ActivityThread
:
public static void main(String[] args) {
...
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
...
Looper.loop();
...
}
The Handler
mechanism is mainly used here . Since this article mainly talks about the Android loading mechanism, it is not too much analysis Handler
. main
There is mainly one method, which is Looper
always waiting for the message ( Message
). main
Most of the methods are Handler
some operations of the main thread . The important thing is:
thread.attach(false);
View attach
method
private void attach(boolean system) {
...
final IActivityManager mgr = ActivityManager.getService(); //1
try {
mgr.attachApplication(mAppThread); //2
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
...
}
1 obtained at IActivityManager
an object mgr
, the object is a system through a Binder
mechanism created a Activity
management class, called by the management system services, and by binder
call interfaces among.
The ApplicationThread
object at 2 mAppThread
is ActivityThread
a member variable, and the class is also ActivityThread
an internal class
private class ApplicationThread extends IApplicationThread.Stub {
public final void scheduleResumeActivity(IBinder token, int processState,
boolean isForward, Bundle resumeArgs) {
...
sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
}
// we use token to identify this activity without having to send the
// activity itself back to the activity manager. (matters more with ipc)
@Override
public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
int procState, Bundle state, PersistableBundle persistentState,
List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
...
sendMessage(H.LAUNCH_ACTIVITY, r);
}
...
}
That mgr
operating system (a system service call), whereas ApplicationThread
only ActivityThread
the inner class, so calling mgr
the attachApplication
relationship so that android application to build the operating system, the operating system after receiving the corresponding event, by ApplicationThread
the method of sending to the app System information . For example, when the user opens the app, the system will mgr
indirectly call scheduleLaunchActivity
this method to Handler
send information to the main thread :
sendMessage(H.LAUNCH_ACTIVITY, r);
View Handler
of the handlerMessage
method
public void handleMessage(Message msg) {
...
switch (msg.what) {
case LAUNCH_ACTIVITY: {
...
final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
} break;
...
}
}
ActivityClientRecord
The object saves the parameters passed by the system, viewhandleLaunchActivity
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
...
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
...
}
...
}
This method mainly executes Activity
the load (Launch) or Resume work, continue to view performLaunchActivity
:
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
try {
...
if (activity != null) {
...
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
...
}
}
return activity;
}
Ways to view mInstrumentation
objectscallActivityOnCreate
public void callActivityOnCreate(Activity activity, Bundle icicle,
PersistableBundle persistentState) {
prePerformCreate(activity);
activity.performCreate(icicle, persistentState);
postPerformCreate(activity);
}
From the above three methods name of view, which is mainly made Activity
of Create
, has Create
front and rear (pre and post) work, see performCreate
Methods
final void performCreate(Bundle icicle, PersistableBundle persistentState) {
...
if (persistentState != null) {
onCreate(icicle, persistentState);
} else {
onCreate(icicle);
}
...
}
To which dawned, Handler
upon receipt of the corresponding information ( Message
executes the operation after), and finally calls Activity
the onCreate
method, thus completing Activity
the Launch
process.