Android Context

在Android开发中常常遇到Context(上下文)这个参数,调用很多函数的时候经常要传递这个参数过去.而默认Android中有好几个函数返回值是Context变量,而这些函数也不知道原理是啥.所以Google搜了一下在stackoverflow上看到有人回答,就顺便贴过来,备忘吧.

getApplicationContext() Application context is associated with the Application and will always be the same throughout the life cycle.

getBasecontext() should not be used just use Context instead of it which is associated with the activity and could possible be destroyed when the activity is destroyed.

getApplication() is available to Activity and Services only. Although in current Android Activity and Service implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that this will always be the case (for example, in a specific vendor implementation). So if you want the Application class you registered in the Manifest, you should never call getApplicationContext() and cast it to your application, because it may not be the application instance (which you obviously experienced with the test framework).

getParent() returns object of the activity if the current view is a child..In other words returns the activity object hosting the child view when called within the child.

在找以上四个区别的时候又发现360DOC上面的Context的介绍文
Android上的Context

不过查一遍我还是没找到区别,我可能还是不知道区别在哪里….

via Blogger http://www.einverne.tk/2013/04/android-context.html

Advertisements

About Ein Verne

獨立之人格 自由之精神
此条目发表在Uncategorized分类目录。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s