Code example for ViewTreeObserver

Methods: isAlive, removeGlobalOnLayoutListener

0
        @Override 
        public void onDismiss() { 
            if (mPopupWindow == null) return; 
            mPopupWindow = null; 
            ViewTreeObserver observer = mAnchorView.getViewTreeObserver(); 
            if (observer.isAlive()) { 
                // We used the deprecated function for backward compatibility 
                // The new "removeOnGlobalLayoutListener" is introduced in API level 16 
                observer.removeGlobalOnLayoutListener(mOnGLobalLayoutListener); 
            } 
        } 
    }; 
 
    private final OnItemClickListener mOnItemClickListener =
            new OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
            if (mPopupWindow == null) return; 
            mPopupWindow.dismiss(); 
            if (mOnPopupItemClickListener != null) { 
                mOnPopupItemClickListener.onPopupItemClick((int) id);