参考了博客:
https://www.jianshu.com/p/95242060320f
但实现与其不同,具体实现代码如下:
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.inputmethod.InputMethodManager;
import java.lang.reflect.Field;
public class InputMethodManagerLastSrvView {
private static boolean sHasField = true;
private static void fixLeak(@NonNull Context context) {
if (!sHasField) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return;
}
String[] arr = new String[]{"mLastSrvView"};
for (String param : arr) {
try {
Field field = imm.getClass().getDeclaredField(param);
field.setAccessible(true);
field.set(imm, null);
} catch (Throwable t) {
t.printStackTrace();
sHasField = false;
}
}
}
}