That's a start, and it's much easier to follow, but that circuit has a problem: the relay coil is powered from the outgoing 5V rail, so when that rail is energised from the 5V power supply OR from the battery, the relay will open!
You need some way to isolate the 5V power supply from the...