It seems to me like there's a rather easy and cheap solution to this problem. Remove any mechanical locks from the cockpit door that can be engaged manually. Install a secure gate between the passenger cabin and the cockpit. Before the pilots step out of the cockpit, have a FA lock that gate. Now either pilot can step out to use the lavatory, but before they do, have them enter their own personalized code into the keypad that prevents the cockpit door from being locked (each pilot has their own code, unknown to the other pilot) until that same code is entered into the keypad again. Once the pilot is back in the cockpit and the door is secure, the FA can unlock that gate.
Now obviously that only works in planes that have a lavatory between the cockpit and passenger cabin. This wouldn't cost much though. It might require an update to the software that controls the keypad on the cockpit door, but that shouldn't be difficult to do. Installation of the gate wouldn't cost much. This will prevent either pilot from ever being locked out of the cockpit and passengers still wouldn't be able to get in