喜糖

移动开发工程师 。涉及 android、ios、jni

导航

activityGroup源码分析

Posted on 2011-09-15 13:48  喜糖  阅读(754)  评论(0编辑  收藏  举报

此前,我们对Activity进行了一些学习,在Android中,还提供了一个ActivityGroup类,该类是Activity的容器,可以包含多个嵌套进来的Activitys,我们接下来依然采用源码分析的方式来了解该类的内部实现。

首先,从SDK中和源码中都可以获知,ActivityGroup类的父类是Activity,也就是说二者具有相同的接口和生命周期,同Activity一样,也有onCreate()onPause()等函数可供我们重载。

ActivityGroup的源码中有成员变量

protected LocalActivityManager mLocalActivityManager;

该变量在ActivityGroup的构造函数中创建并初始化,可见,ActivityGroup的功能实现肯定是要委托给这个对象来完成了。为了给用户开放对此对象的访问,ActivityGroup提供了

    public final LocalActivityManager getLocalActivityManager() {

        return mLocalActivityManager;

    }

通过浏览ActivityGroup的源码可以发现,几乎全部是以通过LocalActivityManager对象来完成的具体动作,比如:

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Bundle states = savedInstanceState != null

                ? (Bundle) savedInstanceState.getBundle(STATES_KEY) : null;

        mLocalActivityManager.dispatchCreate(states);

}

 

下面,我们就来看一下LocalActivityManager的源码。

在该类中,提供了一个私有类

    private static class LocalActivityRecord extends Binder {

        LocalActivityRecord(String _id, Intent _intent) {

            id = _id;

            intent = _intent;

        }

 

        final String id;                // Unique name of this record.

        Intent intent;                  // Which activity to run here.

        ActivityInfo activityInfo;      // Package manager info about activity.

        Activity activity;              // Currently instantiated activity.

        Window window;                  // Activity's top-level window.

        Bundle instanceState;           // Last retrieved freeze state.

        int curState = RESTORED;        // Current state the activity is in.

    }

用于保存Activity的信息,并提供了

    private final Map<String, LocalActivityRecord> mActivities

            = new HashMap<String, LocalActivityRecord>();

    private final ArrayList<LocalActivityRecord> mActivityArray

            = new ArrayList<LocalActivityRecord>();

采用这样的数据结构用于对所有嵌入的子Activity信息进行保存处理。其中前者用于通过String快速查找,后者用于以数组的方式快速访问,是典型的以空间换时间的的方式。

    public void dispatchCreate(Bundle state) {

        if (state != null) {

            final Iterator<String> i = state.keySet().iterator();

            while (i.hasNext()) {

                try {

                    final String id = i.next();

                    final Bundle astate = state.getBundle(id);

                    LocalActivityRecord r = mActivities.get(id);

                    if (r != null) {

                        r.instanceState = astate;

                    } else {

                        r = new LocalActivityRecord(id, null);

                        r.instanceState = astate;

                        mActivities.put(id, r);

                        mActivityArray.add(r);

                    }

                } catch (Exception e) {

……

                }

            }

        }

       

        mCurState = CREATED;

    }

从这里我们可以看出,当有一个ActivityGroupCreate的时候,就会有对应的Activity信息被保存到数组中。

当我们调用LocalActivityManagerstartActivity()以产生Window的时候,我们也可以看到

public Window startActivity(String id, Intent intent) {

                   ……

        LocalActivityRecord r = mActivities.get(id);

        if (r == null) {

            r = new LocalActivityRecord(id, intent);

            adding = true;

        }

                   ……

        if (adding) {

            mActivities.put(id, r);

            mActivityArray.add(r);

        }

        ……

    }

有了这个数组,就可以遍历到ActivityGroup中嵌入的Activitys了,从而可以实现ActivityGroup的功能。

以上的分析结果产生的类图如下: