美文网首页
Context 理解

Context 理解

作者: 大盗海洲 | 来源:发表于2019-05-29 14:46 被阅读0次

继承关系

Main.jpg

Context是个抽象类,在加载资源、启动一个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。

相关文章

  • 源码学习->05Context

    参考文章 : 1. 理解Android Context理解Application创建过程 Context : 1、...

  • Context理解

    官方文档 https://doc.react-china.org/docs/context.html#api 个人...

  • 理解Context

    此文为Android内核剖析学习 Context是啥 一个Context意味着一个场景,一个场景就是用户和操作系统...

  • 理解Context

    什么是Context Context: 字面理解为上下文,语境。Android应用模型是基于组件的应用设计模式,组...

  • 理解Context

    我们平时对Context的理解无外乎两方面: 调用Context的方法,比如启动activity,访问资源; 作为...

  • Context 理解

    继承关系 Context是个抽象类,在加载资源、启动一个Activity、获取系统服务、获取内部文件的路径,及创建...

  • 对 Context 的理解

    对 Context 的理解 1. Context的作用: Context 是应用组件的上下文,有了 Context...

  • HI,Context!

    Context的理解 context的使用场景: getResources() StartActivity() 弹...

  • 进阶之光笔记二

    第五章 理解上下文Context Context的关联类 Context使用场景:1.使用Context调用方法,...

  • context的理解

    转载自:http://blog.csdn.net/singwhatiwanna/article/details/2...

网友评论

      本文标题:Context 理解

      本文链接:https://www.haomeiwen.com/subject/mfejtctx.html