本文共 3880 字,大约阅读时间需要 12 分钟。
Step 2. ActivityStack.activityIdleInternal
这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
- public class ActivityStack {
- ......
-
- final void activityIdleInternal(IBinder token, boolean fromTimeout,
- Configuration config) {
- ......
-
- ArrayList<ActivityRecord> stops = null;
- ......
-
- int NS = 0;
- ......
-
- synchronized (mService) {
- ......
-
-
- stops = processStoppingActivitiesLocked(true);
- NS = stops != null ? stops.size() : 0;
- ......
- }
-
- int i;
-
- ......
-
-
-
- for (i=0; i<NS; i++) {
- ActivityRecord r = (ActivityRecord)stops.get(i);
- synchronized (mService) {
- if (r.finishing) {
- finishCurrentActivityLocked(r, FINISH_IMMEDIATELY);
- } else {
- ......
- }
- }
- }
-
- ......
- }
-
- ......
- }
这个函数首先会调用processStoppingActivitiesLocked函数把所有处于Stopped状态的Activity取回来,然后逐个分析它们,如果它们的ActivityRecord中的finishing成员变量为true,就说明这个Activity需要销毁了,于是,就调用finishCurrentActivityLocked函数来销毁它们。
Step 3. ActivityStack.finishCurrentActivityLocked
这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
- public class ActivityStack {
- ......
-
- private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
- int mode) {
- ......
-
- return finishCurrentActivityLocked(r, index, mode);
- }
-
- private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
- int index, int mode) {
- ......
-
-
- mStoppingActivities.remove(r);
- mWaitingVisibleActivities.remove(r);
- ......
-
- final ActivityState prevState = r.state;
- r.state = ActivityState.FINISHING;
-
- if (mode == FINISH_IMMEDIATELY
- || prevState == ActivityState.STOPPED
- || prevState == ActivityState.INITIALIZING) {
-
-
- return destroyActivityLocked(r, true) ? null : r;
- } else {
- ......
- }
-
- return r;
- }
-
- ......
- }
从上面的Step 2中传进来的参数mode为FINISH_IMMEDIATELY,并且这个即将要被销毁的Activity的状态为Stopped,因此,接下来就会调用destroyActivityLocked函数来销毁它。
Step 4. ActivityStack.destroyActivityLocked
这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
- public class ActivityStack {
- ......
-
- final boolean destroyActivityLocked(ActivityRecord r,
- boolean removeFromApp) {
- ......
-
- boolean removedFromHistory = false;
-
- ......
-
- final boolean hadApp = r.app != null;
-
- if (hadApp) {
- ......
-
- try {
- ......
- r.app.thread.scheduleDestroyActivity(r, r.finishing,
- r.configChangeFlags);
- } catch (Exception e) {
- ......
- }
-
- ......
- } else {
- ......
- }
-
- ......
-
- return removedFromHistory;
- }
-
- ......
- }
在前面一篇文章中,我们说到,每一个应用程序进程在ActivityManagerService中,都Proce***ecord记录与之对应,而每一个Activity,都是运行在一个进程上下文中,因此,在ActivityManagerService中,每一个ActivityRecord的app成员变量都应该指向一个Proce***ecord记录,于是,这里得到的hadApp为true。在Proce***ecord类中,有一个成员变量thread,它的类型为IApplicationThread。在文章中,我们也曾经说过,每一个应用程序在启动的时候,它都会在内部创建一个ActivityThread对象,而在这个ActivityThread对象中,有一个成员变量mAppThread,它的类型为ApplicationThread,这是一个Binder对象,专门用来负责在应用程序和ActivityManagerService之间执行进程间通信工作的。应用程序在启动的时候,就会将这个Binder对象传递给ActivityManagerService,而ActivityManagerService就会把它保存在相应的Proce***ecord记录的thread成员变量中。因此,Proce***ecord记录的thread成员变量其实就是ApplicationThread对象的远程接口,于是,执行下面这个语句的时候:
- r.app.thread.scheduleDestroyActivity(r, r.finishing,
- r.configChangeFlags);
就会进入到ApplicationThread类中的scheduleDestroyActivity函数来。
Step 5. ApplicationThread.scheduleDestroyActivity
这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:
- public final class ActivityThread {
- ......
-
- private final class ApplicationThread extends ApplicationThreadNative {
- ......
-
- public final void scheduleDestroyActivity(IBinder token, boolean finishing,
- int configChanges) {
-
- queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
- configChanges);
- }
-
- ......
- }
-
- ......
- }
这个函数调用外部类ActivityThread的queueOrSendMessage函数来往应用程序的消息队列中发送一个H.DESTROY_ACTIVITY消息,这个消息最终由ActivityThread类的handleDestroyActivity函数来处理。
转载地址:http://kanoa.baihongyu.com/