Forum Archive

Capturing the view close "x"

polymerchm

So, can you capture the event of the "x" in the upper left hand corner of a presented ui.view being touched to allow for a cleanup before the whole script stops? I tried the obvious "finally", but that didn't fire, only the console upper right hand X did that.

ccc

Two techniques to look at: while View.on_screen: and View.wait_modal().

JonB

I assume this is a custom view, in which case, did you try

    def will_close(self):
        # This will be called when a presented view is about to be dismissed.
        # You might want to save data here.
        pass

However, I vaguely recall this working for some types of presentation modes (say, popover), but not others, but I could be wrong about that.

Another alternative to the blocking options that ccc suggests would be a threading.Thread or Timer that polls the on_screen at some reasonably slow interval (few seconds).

One other option... hide the title bar and provide your own "X". Doesn't protect against two finger swipes, but most people don't know about that anyway.