I think it would be better to use onAttachDetach in the widget.