this 和 getApplicationContext Context的區別

轉自:http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context

In various bits of Android code I've seen:
在大量的android代碼中,我看到如下的寫法:

public class MyActivity extends Activity {
    public void method() {
         mContext = this; // since Activity extends Context
         mContext = getApplicationContext();
         mContext = getBaseContext();
     }
}

However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.
不過,我不能找到任何像樣的解釋:那種寫法是最好的,在哪種情況應使用哪種寫法。

Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
如果你有關於這方面的文章的地址,和關於如果選擇錯誤的寫法會產生的錯誤的指導,將不勝感激。

I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.
我承認,涉及android中Contexts的文章鳳毛麟角,但是,我們還是可以從各種來源中拼湊出東西的。

This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
這個谷歌官方的Android開發者寫,主要是爲了幫助解決內存泄漏,的博客文章,同時也給contexts提供了一些良好的信息:

In a regular Android application, you usually have two kinds of Context, Activity and Application.
在一個普通的Android應用程序中,你通常有兩種Context,Activity和Application。

Reading the article a little bit further tells about the difference between to the two and why you might want to consider using the application Context (Activity.getApplicaitonContext()) rather than using the Activity context ("this").Basically the Application context is associated with the Applicaiton and will always be the same throughout the life cycle of you app, where as the Activity context is associated with the activity and could possible be destroyed many times as the activity is destroyed during screen orientation changes and such.
如果再深入讀下這篇文章,它告訴了我們這兩種Context的不同,並且爲什麼你會考慮使用應用程序Context(Activity.getApplicaitonContext())而不是使用活動Context(即"this")。基本上來說呢,應用程序Context是和應用程序關聯的,並且在程序的生命週期內,總是一樣的。但是,活動Context是關聯在某個活動上的,隨着Activity的銷燬(比如橫豎屏切換等情況),這個Context也會被銷燬N多次。

I couldn't find really anything about when to use getBaseContext() other than / from one /(a for a) liner from Dianne Hackborn, one of the Google engineers working on the Android SDK:
我真的沒有找到任何關於使用 getBaseContext()的東西,除了Dianne Hackborn(一位編寫Android SDK的谷歌工程師)的文章中的一句話:

Don't use getBaseContext(), just use the Context you have.
不要使用getBaseContext(),只使用你有的Context。

That was from a post on the android-developers newsgroup, you may want to consider asking your question there as well because a handful of the people working on Android actual monitor that newsgroup and answer questions.
那是來自 android-developers 新聞組的一篇郵件。你也可以考慮在那裏問你的問題,因爲確實有一大幫搞Android的人在管理這個小組,並且回答問題。

So overall it seems preferable to use the global application context when possible.
所以,總體來說,似乎最好在可能的情況下使用全局應用程序Context。

I got bad window token errors when using application context to Progress Dialog. So i used activity context instead.
我使用應用程序Context給進度條對話框的時候,出現了“window token errors”的錯誤。所以,我使用了活動Context,而不是應用程序Context。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章