对Context的理解

对Context的理解

Context关于应用环境的全局信息的接口。是一个抽象类,它的实现是由Android系统提供的(其实就是ContextImpl)。有了它可以访问应用特定的资源和类,并且可以发起一些应用层的调用,例如启动activity,广播,或者接收intents

有哪几种Context?

  1. Application
  2. Activity
  3. Service

Application

  1. 继承关系:Application <- ContextWrapper <- Context
  2. 调用顺序:init -> attachBaseContext -> onCreate
  3. ContextWrapper里包含一个Context即mBase,调用都委托给它了

Activity

  1. 继承关系:Activity <- ContextThemeWrapper <- ContextWrapper
  2. 调用顺序:init -> attachBaseContext -> onCreate

Service

  1. 继承关系:Service <- ContextWrapper <- Context
  2. 调用顺序:init -> attachBaseContext -> onCreate

应用里有多少个Context?不同的Context之间有什么区别?

Activity个数+Service个数+Application个数(多进程)
Activity因为要显示UI,所以需要继承ContextThemeWrapper

Activity里的this和getBaseContext有什么区别?

this返回的是Activity对象自己,它继承的是ContextThemeWrapper。而getBaseContext返回的是ContextWrapper里面的mBase

getApplication和getApplicationContext有什么区别?

都是返回Application对象,getApplicationContext是Context的一个抽象方法,getApplication是Activity和Service特有的

应用组件的构造方法,onCreate、attachBaseContext调用顺序?

init -> attachBaseContext -> onCreate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值