My motto is: keep things simple and elegant! No need for unnecessary complications.
To answer your questions:
- No need for power switching relays and modules, using 2 plain Schottky diodes like 1N5819 will work great if you raise your power supply voltage above the battery pack voltage. Schottky diodes drop half as much voltage as regular rectifier diodes.
- These diodes will also prevent "charging" of the regular AA batteries, which is bad if they're not rechargeable. They could leak or even explode if enough current goes into them. Either way, charging non-rechargeables usually damages them.
- My suggestion is to keep it even simpler than your original switch. You can use a plain push-button switch and simply place it across the switching transistor. Trying to have the MCU boot-up and store an event which is doing at least 2 things in sequence might require more time than the brief moment the switch is closed, and would add to the complexity of the circuit and would be a lot to explain here and I honestly don't know enough at this time to tell you how.
simulate this circuitsimulate this circuit – Schematic created using CircuitLab
I have added the Schottky diodes to prevent battery power from being used while the power supply is on and to also prevent a current back into the battery pack.
I suggest placing a capacitor of maybe 220-1000uF (rated for 16-25V) on the power line to prevent voltage "sag" when solenoid is activated and to ensure solenoid activation. The batteries may have relatively high internal resistance and increasingly so as they drain. This capacitor fixes the problems that would arise due to that fact.
I have placed a simple push-button switch across the transistor. There should be no problems with this arrangement.
I have also increased the value of the base resistor R1 from 2.2 ohms to 100 ohms. 2.2 ohms was too low, and the value of this resistor depends both on the current required for the solenoid and the beta DC factor (amplification) of the transistor. You may also need to use a different transistor if solenoid current is large than 200mA. Let us know what is your solenoid current requirement.
You need to raise the output voltage of your power supply to around 13V to make sure the battery power is not being used when there is AC power available. If you need help with doing this, take a picture of your power supply electronics so that we can figure out how to do that (if it's not adjustable by design).
P.S.: You cancould also use a P-channel MOSFET instead of the D1 diode and connect it where the leads come close inD1 to reduce the schematic (while takingvoltage drop from the diode out and making a break in line there)battery down to almost zero. ThisThe MOSFET would drop almost no voltage and would automaticallyshould turn on whenautomatically as the power supply voltage falls below 8-9Vgoes to zero. Even if it doesn't turn on, the body diode of the MOSFET will conduct as a regular diode.