Here's one way to do it: combine two (or more) batteries in a "wired-OR" configuration. This is fairly generic as you provided no details on your battery voltages. If you want to edit your question to add those details, we'll try to be more specific.
simulate this circuit – Schematic created using CircuitLab
I use Anker phone batteries for projects a lot. I'm sure there are better solutions, but not simpler ones. The big will keep my Pi3 running for at least a day or two. The smaller one might be more practical for you at just $20.
I suspect the answer is to have events rather than a while loop.
Not sure on the logic on this project... Is it: when the button is pressed, check for the phone being connected (or nearby)?
This would mean you could use when_pressed https://gpiozero.readthedocs.io/en/stable/recipes.html?highlight=when_pressed#button
For reading the phone connection, you ...
A 560mAh 3.7V battery contains 2.07Wh of energy. Given you'll need a boost converter that might be 85% efficient, that leaves you with 1.76Wh of energy. A RPi operates at 5V, and the amount of current drawn depends on the model and what it is doing. Around 400mAh is typical for a Pi3B not doing much, so that's 2W (5V * 0.4A). So your battery is going to last ...
If we ignore Peukert's Law for the moment, each one of your batteries (3.7 volt 560 mAH lithium batteries) will run one RPi4b for at most 1 hour. Let's do the math:
RPi4B power consumption ~ 500mA (if lightly loaded)
Time = 560mAh/500mA = 1.12 hours
But this is unrealistically optimistic because:
You'll need a boost converter voltage regulator to get the ...