Android Context - ythy/blog GitHub Wiki

adapter渲染案例:

  1. LayoutInflater.from(getApplicationContext()); 布局不正常, 按钮变大,文字颜色异常
  2. LayoutInflater.from(getBaseContext()); 正常
  • View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.
  • Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.
  • ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().

The method getBaseContext() is only relevant when you have a ontextWrapper. Android provides a ContextWrapper class that is created around an existing Context using:

ContextWrapper wrapper = new ContextWrapper(context);