Some battery-operated devices switch off when plugging in powerless cable or during insertion?
The cable input is wired in parallel with the battery input. If they were allowed to be active at the same time, the batteries would be charged from the cable input. 

The device is designed to be used with non-rechargable batteries. Non-rechargable batteries can leak or explode if you try to charge them. 

Therefore the manufacturer has implemented a mechanical switch inside the power jack, which disconnects the batteries when you plug in the cable.

As Brian mentioned in the comments, these are often break-before-make. In other words the battery circuit is disconnected before the wall power is connected. And when you pull the plug, the battery circuit is not connected until after the wall power is disconnected. This is simply to prevent the user from partially plugging in the power and charging the non-rechargable batteries until something catches fire and the manufacturer gets sued. It's a failsafe.