This can also happen in this extreme case:
If you create a custom view programmatically inside the constructor of another parent view that has the AttributeSet parameter:
public ToggleButtonDescriptive(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.toggleButton = new SquareToggleButton(ctx, attributeSet); }
DO NOT pass the attribute set to this child view:
toggleButton = new SquareToggleButton(ctx, attributeSet);
Since passing the AttributeSet attribute causes the child view to have the same identifier as the parent view, and thus, Android is trying to restore the parent SavedState to that child view or vice versa. Instead, do not specify the AttributeSet parameter at all, for example:
toggleButton = new SquareToggleButton(ctx);
source share