我陷入了那个陷阱,marmor说:)
@Override protected void onDetachedFromWindow() { I want to do something here, sometimes called sometimes not!!} protected void onAttachedToWindow() {It is working fine, always}
此代码位于CustomView 。
调用代码是:
contentHolder.removeAllViews(); // ... init my CustomView ... contentHolder.addView(myCustomView); contentHolder.requestLayout();// useless, not need contentHolder.invalidate();// useless, not need
要了解为什么不起作用,您必须进入Android API:
public void removeAllViews() { removeAllViewsInLayout(); requestLayout(); invalidate(true); } public void removeAllViewsInLayout() { final int count = mChildrenCount; if (count <= 0) { return; } final View[] children = mChildren; mChildrenCount = 0; final View focused = mFocused; final boolean detach = mAttachInfo != null; boolean clearChildFocus = false; needGlobalAttributesUpdate(false); fo