Is there a way to hook up saving UI state to the t...
# compose-web
p
Is there a way to hook up saving UI state to the tab-closing event in browser? I was trying to use
ViewModel.onCleared
from the multiplatform view model to save its state (via KStore), but it didn't work. I want to be able to load the same cached UI state on tab reloading from the Web Storage, what is the best way to go about it?
p
Why not saving it in the remote database?
p
The issue for me is not saving, but when to save. Do i have to auto save my UI state like every minute, or is there a way to only save it when the user closes the tab?
👍 1
✔️ 1
c
you can hook into the beforeunload event
p
Thank you!! Judging from usage notes, hooking up to beforeunload or to visibilitychange would suffice
p
I misunderstood the question, disregard my comment
👍 1
p
update: yep, hooking
visibilitychange
works perfectly, ty for help!
Copy code
document.addEventListener("visibilitychange") {
    if (document["hidden"] == true.toJsBoolean()) {
        // save ui state
    }
}