Holy cow, I wrote a book!
And if you do it wrong, focus will get all messed up.
If you are finished with a modal dialog, your temptation
would be to clean up in the following order:
But if you do that, you'll find that foreground activation doesn't go back
to your owner. Instead, it goes to some random other window.
Explicitly setting activation to the intended owner "fixes"
the problem, but you still have all the flicker,
and the Z-order of the interloper window gets all messed up.
What's going on?
When you destroy the modal dialog, you are destroying
the window with foreground activation.
The window manager now needs to find somebody else to give
It tries to give it to the dialog's owner, but the owner is
so the window manager skips it and looks for some other
window, somebody who is not disabled.
That's why you get the weird interloper window.
The correct order for destroying a modal dialog is
This time, when the modal dialog is destroyed, the
window manager looks to the owner and hey this time
it's enabled, so it inherits activation.