你真的理解 Android 中的 Context 么?
概述 在使用getContext方法的时候有没有想过,在不同的场景下,取到的Context到底有什么不同,View,Fragment,Activity和Application的getContext又究竟是怎么样? 下面来分析一下。 获取Context DecorView DecorView的Context是Application Context。 ActivityThread.addView -> PhoneWindow.generateDecor View 一般View是从LayoutInflater类中inflate生成的,查看inflate方法,会调用rinflate 发现是入参Context,在inflate方法内赋值,其实最后就是LayoutInflater.from的参数Context。 所以结论是普通View是LayoutInflater.from的参数Context。 但是对于xml这样的布局文件里面的View又是怎么样的呢? 看一下Activity.setContentView方法的调用过程是: Activity.setContentView -> Pho...