Android Context
来源:互联网 发布:sql between 边界 编辑:程序博客网 时间:2024/06/06 02:34
一、什么是Context
Context可以理解为上下文环境,大概就是里面存储一堆全局变量,这些变量在调用系统 API时需要用到。
Context持有程序的状态、入口、权限、文件系统、等信息,并且是Activity、Service、Application等组件的父类。通过这些属性,可以获得各种系统的资源。
Activity,Service,Application并没有直接继承Context,而是继承自ContextWrapper。ContextWrapper是Context的包装类,内部包含一个Context的引用,指向Context的具体实现类ContextImpl。ContextWrapper内部的所有方法直接调用ContextImpl对应的方法。
Context 真正实现都是 ContextImpl。
二、context获取方法
mContext = getApplicationContext();
这种方式获得的context是全局context,整个项目的生命中期中是唯一的且一直存在的。
mContext = getContext();
这种方式获得的context当activity销毁时,context也会跟着销毁了。
mContext = getBaseContext();
还没用到
三、context导致的内存泄漏
Activty、Service的 Context都是每次创建的,而不是全局唯一,所以不要将 Activity、Service当做全局 Context引用,这样会导致 Activity无法销毁,一直被引用者。意思就是不要被单例引用。
先了解原理和使用,源码稍后分析.....
- Android - Context, What Context?
- Android Context, What Context?
- android context
- android context
- android context
- Android Context
- Android-Context
- Android Context
- android context
- Android Context
- Android Context
- android Context
- Android Context
- android context
- android context
- android context
- Android Context
- Android Context
- 软工软件生存期模型总结
- 书签备份
- Activity 生命周期、启动模式、IntentFilter 匹配规则
- ios-Swift的只读属性和懒加载对比
- python map函数
- Android Context
- Excel和csv的默认编码
- Oracle12c之 CDB数据库中数据字典架构
- VS项目引用的使用
- React 虚拟DOM
- 2718:移动路线(2.6基本算法之动态规划)
- 自己利用官方OllyDbg配置的2.x全插件版本
- sql中通配符时的使用总结
- Vue