Search code examples
androidviewfocus

The difference between clearFocus() and clearFocusForRemoval()?


I saw android code:

 /**
     * Called when this view wants to give up focus. This will cause
     * {@link #onFocusChanged} to be called.
     */
    public void clearFocus() {
        if (DBG) {
            System.out.println(this + " clearFocus()");
        }

        if ((mPrivateFlags & FOCUSED) != 0) {
            mPrivateFlags &= ~FOCUSED;

            if (mParent != null) {
                mParent.clearChildFocus(this);
            }

            onFocusChanged(false, 0, null);
            refreshDrawableState();
        }
    }
/**
 * Called to clear the focus of a view that is about to be removed.
 * Doesn't call clearChildFocus, which prevents this view from taking
 * focus again before it has been removed from the parent
 */
 void clearFocusForRemoval() {
        if ((mPrivateFlags & FOCUSED) != 0) {
            mPrivateFlags &= ~FOCUSED;

            onFocusChanged(false, 0, null);
            refreshDrawableState();
        }
    }
  1. Can someone explain the difference?
  2. Why not call mParent.clearChildFocus(this); in the clearFocusForRemoval() method?

Solution

  • Don't worry too much about it. Looks like it was a redundant package-private method (or rather not a very useful method - called internally by the view system when a view was getting removed) in release version 4.0.4_r2.1 which was removed in a later version