gpt4 book ai didi

java - ComponentCallbacks2 的 onTrimMemory() 没有被调用

转载 作者:太空狗 更新时间:2023-10-29 15:51:11 31 4
gpt4 key购买 nike

我已经实现了 ComponentCallbacks2 并且永远不会调用 onTrimMemory。我正在尝试使用 Application 类和我的自定义生命周期来管理内存。对此的任何帮助表示赞赏。

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
}

我有一个从 MemoryManager 类调用我的接口(interface)的应用程序类。

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");
}

我的主要 Activity 只是跟踪生命周期。这工作正常。我的生命周期方法如下所示:

@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();
}

调用 onTrimMemory() 我错过了什么??

最佳答案

如果你看the documentation for ComponentCallbacks2 ,您会注意到它已经在 ApplicationActivity 等类上实现。因此,对于这些组件,欢迎您覆盖 onTrimMemory(),它会在适当的时候被调用。

这应该允许您从问题中删除几乎所有代码。

关于java - ComponentCallbacks2 的 onTrimMemory() 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38532661/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com