Android Context原理與使用的總結

一、Context繼承體系 與 Context是如何創建的

  1. Context繼承體系
    只用記住一句:Activity 、 Service 與Application 都是繼承自ContextWrapper,而ContextWrapper implements Context。每個:Activity 、 Service 與Application都是一個Context實例。

  2. Context 何時創建、怎樣創建的 - 查看源碼
    Android應用程序窗口(Activity)的運行上下文環境(Context)的創建過程分析
    Android 內核–Context對象

Android中Context詳解 —- 你所不知道的Context
Context何時創建的,但是對於平時的開發來說,僅需要知道Activity 與Service 都是繼承自Context,只要創建新的Activity 或者 Service 實例,都是創建新的Context實例。
Context 總數 = Activity個數 + Service 個數 + 1個ApplicationContext
可以通過命令行 查看Context的個數
adb shell dumpsys meminfo package_name

二、關於Context的疑問

  1. getBaseContext 與 getApplicationContext 區別?
    持有Activity的Context 相當於持有Context,而持有AppliactionContex全局僅有這一個
  2. 視圖中的Context從哪來的? 例如:new TextView(Context);
    通常在一個Activity中傳入的就是當前Activity或者Activity.getBaseContext(),所以通過View.getContext()其實就是當前Activity的引用。
    常見場景,Adapter通常通過構造器傳遞Context,用於getView 時inflate 視圖。但是getView最有一個參數是parentView 這個是ListView對象本身,可以通過parentView.getContext獲取Context對象減少手動傳遞。
  3. Context 會出錯的地方 Dialog.Builder必須傳入Activity,而不能傳入Activity.getApplicationContext()
  4. Context作用:
    1,查看方法 2,訪問資源、3,創建視圖、4,創建四大組件

Context是什麼?
context是上下文的意思,是運行上下文的一個概述。

參考資料: Android源碼分析-全面理解Context Android中Context的總結及其用法

三 內存溢出,因爲引用Context導致

  1. Context導致內存溢出的原因:
    Avoiding memory leaks 、
    Avoiding memory leaks
    Android - what’s the difference between the various methods to get a Context?

以上文章講解的很詳細可以查看文章,以下是簡單描述: 最常見的內存形式是Bitmap未得到釋放,而圖片通常ImageView持有導致ImageView也不會被GC釋放,創建ImageView肯定需要Context,這個Context是Activity。 Bitmap -> ImageView -> Contex(Activity) 如果Activity總是不能得到釋放,導致內存不足最終OOM ,此處說明按時釋放內存的重要性。

  1. 對於生命週期很長的對象,使用ApplicationContext,以下文檔介紹自定義Application可以在項目全局都很方便獲取Application Context的方法
    使用自定義Application,需要Context對象時傳入,避免因持有Context導致的內存溢出。因爲ApplicationContext全局僅有一個實例,而多個Activity本身繼承自Context,就是多個Context實例。
    Android中Activity共享變量的另一方法:Application context
    談談Android裏的Context的使用!

四、自己創建Context
大家應該養成一個習慣,比如使用Baseactivity環境下創建context,同時要注意只要有activty或者service等組件就建立當前的context變量,這樣方便引用,同時防止過多依賴context的組件引起相關綴餘。

分清楚context ,getbaseContext, getappliCationcontext, activty.this等情況,同時注意context和其他contextwraper組件之間的相關性練習。
未完待續。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章