Component "before detach"

Hi all!

The basic problem is: I’d like to show up a confirm dialog before the form that the user used would be removed, so the user could decide to stay on that “page”.
Is there any way to do this in the proper way? with listeners etc. I’ve tried it with the detach listener, but that only runs after the detach is completed.

Thank you for your help!