Which tool are you working with when this happens? If you are using the Move Tool, there is an option that is enabled by default called "Auto Select Layer". This will cause the topmost layer with opaque pixels under you mouse to be selected. You may want to disable this option from the Tool Options bar at the top of the Editor (just under Menu Bar and Shortcut Buttons Bar).
Also, changing the selected layer in the Layers panel is not recorded in your undo history. So if you have changed the active layer and then undo, it will change back to the layer that was selected during the last history state. For example, if you use the Paint Bucket to fill one layer, switch to a different layer, and then undo the Paint Bucket, you will be selecting that first layer again.