征服Android面试官路漫漫(三),rrxjava原理

调用了 ContextImpl.createActivityContext() 方法。

static ContextImpl createActivityContext(ActivityThread mainThread,

LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId,

Configuration overrideConfiguration) {

// 创建 ContextImpl 对象

ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityInfo.splitName,

activityToken, null, 0, classLoader);

final ResourcesManager resourcesManager = ResourcesManager.getInstance();

context.setResources(resourcesManager.createBaseActivityResources(activityToken,

packageInfo.getResDir(),

splitDirs,

packageInfo.getOverlayDirs(),

packageInfo.getApplicationInfo().sharedLibraryFiles,

displayId,

overrideConfiguration,

compatInfo,

classLoader));

context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,

context.getResources());

return context;

}

装饰类 ContextWrapper 真正需要的 ContextImpl 对象现在已经创建出来了,但是还没有绑定到 Activity 。继续看 Activity.attach() 方法,注意attach() 方法的第一个参数就是刚刚创建出来的 ContextImpl 对象。

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) {

// 回调 attachBaseContext()

attachBaseContext(context);

// 创建 PhoneWindow

mWindow = new PhoneWindow(this, window, activityConfigCallback);

mWindow.setWindowManager(

(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),

mToken, mComponent.flattenToString(),

(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);

}

你对 attachBaseContext() 方法应该还有印象。ContextWrapper 正是通过这个方法给 mBase 对象赋值,拿到真正的 ContextImpl 对象。到这里,整个逻辑就通顺了。

注意 attach() 方法中的 setWindowManager() 方法中的 mToken 参数,这决定了 Application Context 无法创建和显示 Dialog 。后续会进行详细分析。

再回头看看文章开头的问题。

Log.e(“context”, "getApplication in Activity: " + getApplication().getClass().getName());

Log.e(“context”, "getApplicationContext in Activity: " + getApplicationContext().getClass().getName());

Log.e(“context”, "getBaseContext

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值