One way to solve this problem is, when the second modal is closed, forcing the mouse click on the current modal using javascript.
How can you not know what the current modal, use the position (center of screen) to find the active field.
Something like this
function ClickCenterScreen() {
x = $(document).width() / 2;
y = $(document).height() / 2;
element = $(document.elementFromPoint(x, y));
element = $(element).contents().find("form").find("input[type=text]").first();
// you need to set focus on element first.
$(element).focus();
$(element).click();
}