If you go into a bathroom looking to shit, you want an empty bathroom, if someone is in said bathroom occupying a stall don't pretend to blow your nose, wash your hands or what have you, leave. Know that if you were in there position they would want you gone.
If you go into the bathroom to wiz, and someone is in a stall, hurry and wiz. Then leave. Don't talk to anyone and don't wash your hands. Just know that if you were in that stall you would the person coming in to leave ASAP.
If you find yourself in a stall, only to have someone enter the stall adjacent to you, this is a phenomenon known as quantum lock, you are not responsible for this, and have the restroom right of way. remember the fundamental attribution error however, and don't be a dick to him.
Empathy and sympathy are the biggest parts of public restroom behavior.