What is the proper way of keeping track of menus, and using that information to create a back button?

What is the proper way of keeping track of all the menus screens scripting wise, and using that to make a back button. I want to be able to use a key to go back instead of clicking on ui. On the UI you have to manually put in what panel is about to show and which panel you are hiding. With a key down, I am not sure how that works, because there is no definite info to place in since you could be backing out of multiple panels. I’ve read through many unity docs, and searched most of this evening. I know how to write the key down part. It’s for a pc.

Use the stack to store “active” menus.

When opening a menu, add it to the stack, and it will be overlaid on top of other menus. You can add a few options such as disable or disable the interaction of the menu below.

When closing a menu, delete it from the stack, obviously can only close the menu at the top. And you open it in any order, it will close it in reverse order. Also remember that the menus below may have been disable or turned off interactivity.

So the back button simply closes the menu on top.