Skip to content

3.2 Using Screen Properties

Pieter van Leeuwen edited this page May 8, 2015 · 1 revision

Screen Properties

Screen properties allow a developer to save screen specific data. By default these variables are not persistent (they will not be retrieved when restoring the screen), however they can be made persistent by using the HTML5Application.addToRecoveryList() method. Please check chapter(?) for more information on HTML5Application.addToRecoveryList().

Storing data

Data can be stored by using the Screen.setProperty(String key, Object value) method.

Example: Storing searchword

s.setProperty("searchword", "cars");

Data stored with s.setProperty(String key, Object value) will be saved in the current screen state, if the screen is destroyed and restored, it will not retrieve the data for this property during restoration.

Retrieving data

Example: Retrieving searchword

String s = (String) s.getProperty("searchword");

You will have to remember yourself what type of data your variable was, and cast the value into the correct data type (in this case String).

Example: Storing and retrieving a more complex datastructure

//Create new HashMap that contains usercolors
Map<String, String> userColors = new HashMap<String, String>();
//Lets add some data
userColors.put("pete", "#54IKF4");
userColors.put("anne", "#62FFI1");

//Save the HashMap to the screen
s.setProperty("userColors", userColors);

//Lets retrieve the userColors
userColors = (HashMap<String, String>) s.getProperty("userColors");

Making Screen properties persistent

There are times when you want to make a certain variable persistent, so that you can use it after the restoration of a Screen. An example of this might be storing user credentials, so that a user doesn't have to log in every time they open a screen.

You can make screen variable persistent by adding them to the screen variables recovery list of the application. This recovery list is reachable from the application. The following example shows how to make a variable persistent.

Currently you can only make a String persistent.

Example: Making a query persistent

ExampleApplication.java

public class MyeuscreenApplication extends Html5Application {

    public MyeuscreenApplication(String id){
        super(id);
        
        //This indicates that want to activate session recovery functionality for this application.
        this.setSessionRecovery(true);

        //This indicates that we want to make the screen property "query" persistent. 
        this.addToRecoveryList("query");
    }

    //This function is called when a new screen is created, if a screen has already been opened on the current device, it will restore the persistent properties of the previous time the screen was opened.
    public void onNewScreen(Screen s) {
        String query = (String) s.getProperty("query");
        System.out.println("THE QUERY = " + query);
    }
    
    //This is a function that is called from the client. Saves the current query to the screen object.
    public void setQueryForScreen(Screen s, String query){
        s.setProperty("query", query);
    }
}

More complete example

A working example app that uses session properties can be found here:

https://github.com/Noterik/smt_sessionexampleapp

Clone this wiki locally