Home / Java Patterns and Pitfalls     frequal.com

TeaVM: Storing App Preferences in the Browser

So you've got a great app you've written with TeaVM. However, there is a setting you want to remember client-side, a value the user customizes and you don't want to force them to re-enter each time they restart the app. One option would be to store this server-side, but let's say this value is custom to each device they are using. Then the easiest option is client-side storage.

Using the Storage API

Using the Storage API is easy. Just get a reference to the local storage singleton, then save strings under a string key.
    // Saving preferred font size
    Storage storage = Storage.getLocalStorage();
    storage.setItem("fontSize", Integer.toString(preferredFontSize));
When you need to read the value back, use getItem():
    String fontSize = storage.getItem("fontSize");

Complex Data? Use the Serialization API

Flavour provides a serialization API. You can turn a Java object into a string, and back again. This lets you store complex objects in Storage.

More about TeaVM

Read these articles about TeaVM:
Last modified on 5 Aug 2018 by AO

Copyright © 2018 Andrew Oliver