Back button changes state when popup is visible

I have two popups, where the second one is called in the first’s onTap function. The problem is that when the first one closes and the second one opens, if you tap the Android hardware back button, the app changes state and the popup stays visible.