使用:BaseActivity中的OnDestory
public class InputMethodManagerUtil {
public static void fixInputMethodManagerLeak(Context destContext) {
if (destContext == null) {
return;
}
try {
InputMethodManager imm = (InputMethodManager) destContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return;
}
String[] arr = new String[] { "mCurRootView", "mServedView", "mNextServedView" };
Field f = null;
Object obj_get = null;
for (int i = 0; i < arr.length; i++) {
String param = arr[i];
f = imm.getClass().getDeclaredField(param);
if (f.isAccessible() == false) {
f.setAccessible(true);
}
obj_get = f.get(imm);
if (obj_get != null && obj_get instanceof View) {
View v_get = (View) obj_get;
if (v_get.getContext() == destContext) { // 被InputMethodManager持有引用的context是想要目标销毁的
f.set(imm, null); // 置空,破坏掉path to gc节点
} else {
// 不是想要目标销毁的,即为又进了另一层界面了,不要处理,避免影响原逻辑,也就不用继续for循环
break;
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
修复输入法内存泄漏
本文提供了一种在Android应用中防止因输入法(InputMethodManager)引起的内存泄漏的方法。通过检查并清除特定条件下不再需要的视图引用,确保在Activity销毁时能够正确释放资源。
1349

被折叠的 条评论
为什么被折叠?



