不BB,直接上代码
MemoryUtils.java
public static void releaseInputMethodManagerFocus(Activity paramActivity) {
if (paramActivity == null) return;
int count = 0;
while (true) {
//给个5次机会 省得无限循环
count++;
if (count == 5) return;
try {
InputMethodManager localInputMethodManager = (InputMethodManager) paramActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (localInputMethodManager != null) {
Method localMethod = InputMethodManager.class.getMethod("windowDismissed", new Class[]{IBinder.class});
if (localMethod != null) {
localMethod.invoke(localInputMethodManager, new Object[]{paramActivity.getWindow().getDecorView().getWindowToken()});
}
Field mLastSrvView = InputMethodManager.class.getDeclaredField("mLastSrvView");
if (mLastSrvView != null) {
mLastSrvView.setAccessible(true);
mLastSrvView.set(localInputMethodManager, null);
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在有内存泄露的activity的onDestroy方法里面,调用上面releaseInputMethodManagerFocus方法即可
@Override
protected void onDestroy() {
MemoryUtil.releaseInputMethodManagerFocus(this);
super.onDestroy();
}