I'm using the following code to detect whether or not the keyboard is showing throughout the lifetime of an activity:
rootVTO = root.getViewTreeObserver();
keyboardListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (root.getRootView().getHeight() - root.getHeight() > 150) {
// Keyboard showing
isKeyboardShowing = true;
} else {
// Keyboard not showing
isKeyboardShowing = false;
}
refreshLayout();
};
};
rootVTO.addOnGlobalLayoutListener(keyboardListener);
I tried to remove the listener in the onPause() method but get the message:
Caused by: java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver()
Will there be a memory leak if I don't remove the listener? If so when should I remove it? Is it sufficient to just set the listener to null?