Hello,
I am currently trying to find out the best and most efficient way to make sure local variables are refreshed when going to the previous screen through the back button of the browser.
From what I've read online it is standard functionality that local values are maintained when navigating to a previous screen (either through browser buttons or destination "previous screen" node).
But what is the best way to make sure the variables are loaded to defaults like normal navigation?
Some of the findings and conclusions from my side (feel free to respond on these too):
I hope someone has some insight in this, thank you!
Hi @Sten Mols,I agree with @Sherif El-Habibi that the best and most robust approach is to control the screen state through the lifecycle (OnInitialize / OnRender) and keep the initialization logic deterministic.
If you absolutely need to force a fresh load when the user navigates back, you could use the View API: Javascript API View
In OnRender you can check in JavaScript whether the view was restored from cache and trigger a hard reload:if ($public.View.wasCurrentViewRestoredFromCache()) { window.location.reload();}
I have not used this approach in production, so I cannot comment on possible limitations or side effects. It is just an idea based on the documentation rather than a proven pattern.
Hi @Sebastian Ośkiewicz,
Ah that sounds very interesting, I will look into it!
Thank you for thinking with me on this
Hello @Sten Mols,
I believe this can only be handled at the screen level. I don’t think there is an application-level solution for this.
It can be implemented in OnRender or OnInitialize events by resetting the local variables to their default values. I understand you mentioned having many screens, which makes this approach less ideal, but technically it seems to be the only available option.
As for the idea of using an event listener in the layout to catch the browser back button and navigate yourself first, I’m not entirely sure about that approach.
As far as I know, most websites preserve values when the browser back button is used, so this behavior is actually expected. As a workaround, you could rely on the back button within the web application itself instead of the browser back button to have more control over the screen state.
Just a correction to avoid any misunderstanding or mislead:
“It can be implemented in OnRender or OnInitialize” should actually be OnReady or OnInitialize.
If it is implemented in the OnRender event, every data refresh (in other words any change of the data of the Screen such as an Aggregate or Data Action refresh) would reset the values, which is not the intended behaviour.
Therefore, it should be implemented either in OnInitialize or OnReady meaning at the start of the screen initialization or once the DOM is fully rendered.
@Sherif El-Habibi Thx, that makes sense!
Hi @Sten Mols ,
I faced it in 2 ODC projects
My approach is to reassign the default value in OnInit and store it in a client variable if filter storage is needed.
I won't be using the default value attribute of the variable.