2012-10-03 3 views
1

여기서 볼 수있는 "Google I/O 2012 - 더 안 좋은 것을하는 좋은 Android 시민"이야기 듣기 http://www.youtube.com/watch?v=gbQb1PVjfqM&list=PL4C6BCDE45E05F49E&index=10&feature=plpp_video api 레벨 14부터 onTrimMemory을 무시할 수 있다는 것을 알았습니다. 메모리 사용량을 줄이는 것과 같은 일을하고 싶습니다. 하지만 내 CursorAdapter 인스턴스 내에서 내 Activity 외부에 ComponentCallbacks2 인터페이스를 구현하고 싶습니다. 이 대화에서 : Context.registerComponentCallbacks()을 사용합니다. 그러나 이것을 사용하려고하면 입력 매개 변수가 필요합니다. mContext.registerComponentCallbacks(ComponentCallbacks callbacks);android onTrimMemory and ComponentCallbacks2

어떻게 사용할 수 있습니까?

지금은 결코 호출되지 도착이

public class ContactItemAdapter extends ResourceCursorAdapter implements ComponentCallbacks2{ 
... ... 
    @Override 
    public void onTrimMemory(int level) { 
     Log.v(TAG, "onTrimMemory() with level=" + level); 

     // Memory we can release here will help overall system performance, and 
     // make us a smaller target as the system looks for memory 

     if (level >= TRIM_MEMORY_MODERATE) { // 60 
      // Nearing middle of list of cached background apps; evict our 
      // entire thumbnail cache 
      Log.v(TAG, "evicting entire thumbnail cache"); 
      mCache.evictAll(); 

     } else if (level >= TRIM_MEMORY_BACKGROUND) { // 40 
      // Entering list of cached background apps; evict oldest half of our 
      // thumbnail cache 
      Log.v(TAG, "evicting oldest half of thumbnail cache"); 
      mCache.trimToSize(mCache.size()/2); 
     } 
    } 
} 

하지만 onTrimMemory을 사용하고 있습니다.

+0

이 개체의 mCache는 –

답변

8

어댑터 클래스는 이미 ComponentCallbacks2를 구현하므로 어댑터 인스턴스를 인수로 registerComponentCallbacks()에 전달할 수 있어야합니다.

활동에서

,이 같은 작업을해야합니다 :

registerComponentCallbacks(mAdapter); 

그 후에는 onTrimMemory() 콜백을 받아야합니다.

+0

입니다. 예. 고마워! – Sandra

+0

어떻게 내 프래그먼트에서 registerComponentCallbacks (mAdapter) 메서드를 호출할까요? –

+1

@HelalKhan 필요한 것은 컨텍스트입니다. Fragment에서 다음과 같이 사용할 수 있습니다 :'getActivity(). registerComponentCallbacks (...)'. – acj