With the help of the generous advice of the people on this forum I have written a image extension. So far, so good, so happy.
Let's look at the work flow: Say, in the Files app I have selected some images, then go to Share --> Pyhonista --> My Script and then the Image UI opens:

Now when I decide I want close the UI I do this via the Close button (circled in the screenshot). However the Pyhonista script selector stays visible then. Programmatically I know I can dismiss it with appex.finish(), but how can I hook this call up to the Close button?
Any idea?