Reputation: 4259
Listening to the "Google I/O 2012 - Doing More With Less: Being a Good Android Citizen" talk, which you can see here http://www.youtube.com/watch?v=gbQb1PVjfqM&list=PL4C6BCDE45E05F49E&index=10&feature=plpp_video i found out that since api level 14 you can override onTrimMemory
if you like to do some things like reducing the memory usage. But I want to implement the ComponentCallbacks2 interface outside my Activity, in my CursorAdapter for instance. In this talk it says: use Context.registerComponentCallbacks()
, but when I try to use this it requires an input parameter, like this mContext.registerComponentCallbacks(ComponentCallbacks callbacks);
How can I use this?
Now I am using this
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);
}
}
}
but onTrimMemory
never gets called.
Upvotes: 1
Views: 8752
Reputation: 4821
Since your Adapter class implements ComponentCallbacks2 already, you should be able to pass the Adapter instance as the argument to registerComponentCallbacks()
.
From your activity, something like this should work:
registerComponentCallbacks( mAdapter );
After that, you should receive onTrimMemory()
callbacks.
Upvotes: 8