What happens when one of them needs to use the restroom varies by airline procedure and the aircraft type. If one of the pilots steps out the cockpit door is locked behind him. The other pilot must remain seated at the controls with his oxygen mask on. On a small plane like a commuter jet the remaining pilot can reach backwards to let the other pilot back in when he has finished his business. On the larger jets the door may be too far behind him so an FA will sit in the cockpit, not because she is expected to be able to assist flying, but just to let the other pilot back in the cockpit.
You will also notice that some airlines will use a beverage cart to block the aisle while the cockpit door is momentarily open to stop a passenger from rushing the door.
Hope this answers your question.
Thanks for the info.