继承关系
Main.jpgContext是个抽象类,在加载资源、启动一个Activity、获取系统服务、获取内部文件的路径,及创建一个View都有他参与。
/**
* Interface to global information about an application environment. This is
* an abstract class whose implementation is provided by
* the Android system. It
* allows access to application-specific resources and classes, as well as
* up-calls for application-level operations such as launching activities,
* broadcasting and receiving intents, etc.
*/
//有关应用程序环境的全局信息的接口。 这是一个抽象类,其实现由Android系统提供。
//允许访问特定于应用程序的资源和类,以及用于应用程序级操作的调用,例如启动活动,广播和接收意图等。
public abstract class Context {
public abstract Resources getResources();
public abstract void startActivity(@RequiresPermission Intent intent);
······
}
/**
* Common implementation of Context API, which provides the base
* context object for Activity and other application components.
*/
//Context API的通用实现,它为Activity和其他应用程序组件提供基本上下文对象。
class ContextImpl extends Context {
}
/**
* Proxying implementation of Context that simply delegates all of its calls to
* another Context. Can be subclassed to modify behavior without changing
* the original Context.
*/
//代理Context的实现,简单地将其所有调用委托给另一个Context。 可以子类化以修改行为而无需更改原始上下文。
public class ContextWrapper extends Context {
Context mBase;
public ContextWrapper(Context base) {
mBase = base;
}
/**
* Set the base context for this ContextWrapper. All calls will then be
* delegated to the base context. Throws
* IllegalStateException if a base context has already been set.
*
* @param base The new base context for this wrapper.
*/
//设置此ContextWrapper的基本上下文。 然后将所有调用委托给基本上下文。
//具体委托给ContextImpl实现Context具体的功能
protected void attachBaseContext(Context base) {
if (mBase != null) {
throw new IllegalStateException("Base context already set");
}
mBase = base;
}
}
/**
* A context wrapper that allows you to modify or replace the theme of the
* wrapped context.
*/
//一个上下文包装器,允许您修改或替换包装上下文的主题。
public class ContextThemeWrapper extends ContextWrapper {
@Override
protected void attachBaseContext(Context newBase) {
//调用父类ContextImpl
super.attachBaseContext(newBase);
}
}
public final class ActivityThread {
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
ContextImpl appContext = createBaseContextForActivity(r);
Activity activity = null;
try {
java.lang.ClassLoader cl = appContext.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
r.intent.prepareToEnterProcess();
if (r.state != null) {
r.state.setClassLoader(cl);
}
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component
+ ": " + e.toString(), e);
}
}
try {
//appContext-->ContextImpl
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback);
}catch(Exection e){
}
}
}
public class Activity extends ContextThemeWrapper{
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback) {
//添加ContextImpl
attachBaseContext(context);
}
}
Context 与Application
public class MainActivity extends AppCompatActivity {
Context baseContext = getBaseContext();
Context context= this;
Application application = getApplication();
}
log
D/TAG: baseContext:android.app.ContextImpl@658836a
D/TAG: context:d.MainActivity@ee697dc
D/TAG: application:android.app.Application@d52a6bd
this 的Context 是指当前的activity对象,生命周期是和当前的activity绑定在一起,若this 被static 修改的方法或者匿名内部等对象持有,周期短的被周期长的持有,解绑不当,会有内存泄漏,这时候可以使用周期和应用一样长的Application。
网友评论