While most people don't think of these things, and others laugh, many police academies give specific training on this.
There are many stories of police officers sitting in the john only to have someone reach over the partition and grab a gun off the coat hook, or under the stall and grab a gun off the floor.
Thieves have been known to follow cops into the john for a chance at a gun, or just be looking under the stalls to see what they could grab and see a gun.
In any case, the gun owner is in no condition to dash out and chase the thief.
Many departments train cops to remove the gun, and while sitting, place the gun in the "sling" formed by the crotch of your pants.
This holds it securely, and keeps it off the floor, out of sight of sneak thieves.
Most departments train cops to NEVER sit there, holding the gun in your hand.
Stupidly, people don't pay attention, and accidentally fire the gun.
Even putting the gun on the floor between your feet isn't good, since no one ever expects someone to just reach under a stall that far to grab something, but thieves do it all the time.
This happens so fast, even though it's between your feet, it's gone before you can react.
Hanging it on the door hook is worse, since the thief is on his feet and already has a running start.
Another hazard of the door hook, is the potential for an AD.
One cop had an incident when he hung his Glock on the door hook by the trigger guard.
When he reached up to take it off, he inadvertently pushed it upward, activating the trigger.
The gun fired, and he lost his grip.
Under recoil, the gun spun around on the trigger guard, and the trigger was activated again.
The gun basically went full-auto as it spun around the hook by the trigger guard emptying the magazine.
Fortunately no one was hurt, but the cop never again stored his gun on a hook.
So, best technique: Remove the entire holster if possible, and just the gun if not, sit down, form a sling with your trousers, and put the gun in that "pocket".