Maybe the problem is that when the button is pressed, new instances of the class are created, like here?
They are created when I click the button, then written to a variable (The idea is that if this variable is already valid, then do not create new instances). But as soon as I close this widget, then reopen it, click on the button, a new instance is created.
You only create it once and reference it. It can then remove itself. When you click again, you use the existing reference to bring back the existing instance. It will retain the data.