第五章 理解上下文 Context.md

1021

5.1 Context 的关联类

Context 是一个应用程序环境信息的接口,我们经常在两大场景使用它:

  • 使用 Context 调用方法
  • 调用方法时传入 Context

image20210928155150124.png

Context 的关联类采用装饰者模式,其中 ContextWrapper 为装饰类,具体方法都由其内部 mBase 变量实现,该变量为 ContextImpl 类型,同时它们又一起实现了 Context 接口 。最后 Activity Service 与 Application 都继承 ContextWrapper ,需要主题的 Activity 继承 ContextWrapper 的子类 ContextThemeWrapper 。

接下来根据各个 Context 的创建过程来分析:

5.2 Application Context 创建过程

image20211001133048376.png

5.3 Application Context 获取过程(PASS)

5.4 Activity 的 Context 创建过程

image20211001133726563.png

5.5 Service 的 Context 创建过程

该过程与 Activity 的 Context 创建过程基本一致。