Coder Social home page Coder Social logo

shiguiyou.github.io's People

Contributors

shiguiyou avatar

Watchers

 avatar  avatar

shiguiyou.github.io's Issues

Context相关知识点

Context is the bridge between components. You use it to communicate between components, instantiate components, and access components.

我们使用context去初始化Activity, Content Provider, BroadcastReceiver等等,我们也是用它获取资源resource或者文件系统。

Context作为安卓系统的一个入口点,比如去获取系统的WifiManager, Vibrator, and PackageManager. context.getSystemService(Context.WIFI_SERVICE)

Let’s agree that everything in Android is a component. Context is the bridge between components. You use it to communicate between components, instantiate components, and access components. I hope the definition is now clear.

I divide context types into two categories: UI Context and Non-UI Context.

In reality, only the ContextThemeWrapper is UI Context — which means Context + Your theme.

UI Context:

- Activity
	- Instance of your activity (this)
- Fragment
	- getContext() in Fragment
- View
	- getContext() in View (if View was constructed using UI-Context)

Non-UI Context is allowed do almost everything UI-Context can do (bad design spotted). But as we pointed out above, you lose theming.

Non-UI Context

- Application instance as context
- Activity
	- getApplicationContext() in Activity
- Broadcast Receiver
	- Context received in broadcast receiver
- Service
	- Instance of your service (this)
	- getApplicationContext() in Service
- Context
	- getApplicationContext() in Context instance

Summary

  • Do you need to access UI related stuff? Use UI-Context. Inflating Views and showing dialogue are the two use cases I can think of.
  • Otherwise, Use Non UI Context.
  • Make sure you do not pass short-living context to long-living objects.

Context

Context类本身是一个纯abstract类,它有两个具体的实现子类:ContextImpl和ContextWrapper。其中ContextWrapper类,如其名所言,这只是一个包装而已,ContextWrapper构造函数中必须包含一个真正的Context引用,同时ContextWrapper中提供了attachBaseContext()用于给ContextWrapper对象中指定真正的Context对象,调用ContextWrapper的方法都会被转向其所包含的真正的Context对象。

参考

Mastering Android context
Context都没弄明白,还怎么做Android开发?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.