Issue
I have implemented ComponentCallbacks2 and onTrimMemory is never getting called. I am trying to manage memory using the Application class and my custom lifecycle. Any help with this is appreciated.
public class MemoryManager implements ComponentCallbacks2 {
private static List<MemoryInfo> memInfoList = new ArrayList<>();
public interface MemoryInfo {
void releaseMemory();
}
public static void registerMemoryListener(MemoryInfo memoryInfo) {
memInfoList.add(memoryInfo);
}
public static void unregisterMemoryListener(MemoryInfo memoryInfo) {
memInfoList.remove(memoryInfo);
}
@Override
public void onTrimMemory(int level) {
Log.i("TEST", "onTrimMemory called"); // does not get called
switch (level) {
case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
try {
for (int i = memInfoList.size() - 1; i >= 0; i--) {
try {
memInfoList.get(i).releaseMemory(); // is this correct implementation?
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
// I added logs here, it does not get reached
break;
default:
break;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// do nothing
}
@Override
public void onLowMemory() {
// will log when there is log memory
}
I have an application class that calls my interfaces from MemoryManager class.
public class TestApplication extends Application implements ActivityLifecycleCallback, MemoryManager.MemoryInfo {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Activity activity) {
Log.i(TAG, "onStart() called :: " + activity.getLocalClassName());
}
@Override
public void onResume(Activity activity) {
Log.i(TAG, "onResume called :: " + activity.getLocalClassName());
MemoryManager.registerMemoryListener(this);
}
@Override
public void onPause(Activity activity) {
Log.i(TAG, "onPause called :: " + activity.getLocalClassName());
}
@Override
public void onStop(Activity activity) {
Log.i(TAG, "onStop called :: " + activity.getLocalClassName());
MemoryManager.unregisterMemoryListener(this);
}
@Override
public void onDestroy(Activity activity) {
Log.i(TAG, "onDestroy called :: " + activity.getLocalClassName());
}
@Override
public void releaseMemory() {
Log.i(TAG, "releaseMemory() called");
}
My main Activity is just keeping track of the lifecycle. This is working fine. My lifecycle methods look like the following:
@Override
protected void onDestroy() {
// register lifecycle
application.onDestroy(activity);
super.onDestroy();
}
@Override
protected void onPause() {
// register lifecycle
application.onPause(activity);
super.onPause();
}
@Override
protected void onResume() {
// register lifecycle
application.onResume(activity);
super.onResume();
}
@Override
protected void onStart() {
// register lifecycle
application.onStart(activity);
super.onStart();
}
What am I missing to invoke onTrimMemory()??
Solution
If you look at the documentation for ComponentCallbacks2, you will notice that it is already implemented on classes like Application and Activity. So, for those components, you are welcome to just override onTrimMemory(), and it will be called as appropriate.
This should allow you to delete just about all the code from your question.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.