How to preserve a widget state on refresh?

How can i preserve a widget state on refresh?

Generally a widget (and so also its state) will stay alive as long as it is refernced somewhere. When your UI is set to PreserveOnRefresh and you refresh the page all widget on it stay how they were at the point when you pressed Refresh.
As soon as you remove a component from a layout or the entire Layout, the Session gets closed, … the widget gets unreferenced and in the end garbage collected.

The UI is well set to PreserveOnRefres, when i refresh the page the widget referenced on the UI is reconstructed and lost her last state.

How have you referenced the widget?
Is the entire UI rebuild when you refresh? If yes do you maybe have restartApplication in the Browser URL?
Is init called again when you refresh the page for you?

The widget was added on the UI like a component in a simple Panel, on refresh just the widget is reconstructed and lost his state, The rest of the UI preserv his state as it is set to preservOnRefresh.

That’s weird. Is it a custom widget?
Do you maybe have code somewhere which might get fired on a Page refresh causing a redraw and reinitialization of the widget?

No, any where.

  1. What Vaadin version are you using?
  2. If it’s a custom widget, are you using ComponentState or RPC to sync state?
  3. Did you already try to just create a basic widget by just doing New… → Vaadin Widget and then test it with this one? (By clicking the label a few times and then reload. This worked like a charm when i tried it)
  1. I’m using Vaadin 7.1.13
  2. To sync state I’m using ComponentState
  3. I have tested the basic widget and it have the same behavior (state lost on refresh)

Not sure if this is your problem, but your symptoms at least seem to match. State objects should be treated as read-only on the client side. If you need to update the component state based on an event on the client side, you should make an RPC call to the server with the new value and then on the server side set the new value to the state. Only this way will the server be aware of the new state and thus the state is preserved even on refreshes.

Yes, I’ll try this way and inform you of results

It is well done, Thinx for your help.