标签:android style blog http color io 使用 ar java
在开发中Application类,几乎每个项目中都会使用到,它是优化于四大组件运行的,它是一个全局的盒子,可以这么理解,比如我们可以在application类中存放一些变量,在其他类中可以获取到,比如在A类中把变量存放在Appliaction类中,然后在B类中可以获取,但是在Application中存放静态的变量,容易被系统回收,今天讲的是Application与Context的关系
请看图:
从图可以看出Application是Context的子类,
从java继承的关系看,子类的功能一定要比父类的功能强大,我们在项目中很多地方都会用到Context这个对象,一种方法是通过方法传递,一种方法是通过控件的方法获取,在android中常用的布局对象也可以获取,比如:LinearLayout ,RelativeLayout FrameLayout,都提供了getContext()方法获取Context对象,还有还有一种就是直接在Application类中直接使用了,
public class BaseApplication extends Application { public static BaseApplication mInstance; @Override public void onCreate() { super.onCreate(); mInstance = this; } public static BaseApplication getApplication(){ return mInstance; } }因为Application是Context的子类,所以直接获取到Application就可以
标签:android style blog http color io 使用 ar java
原文地址:http://blog.csdn.net/coderinchina/article/details/39890109