×
Namespaces

Variants
Actions
Revision as of 13:51, 26 February 2009 by croozeus (Talk | contribs)

Storing settings in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search


Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
S60 5th Edition
S60 3rd Edition FP2
Article
Keywords: setPreferenceForKey, preferenceForKey
Created: (27 Jan 2009)
Last edited: croozeus (26 Feb 2009)


Overview

This code snippet shows how to use methods widget.setPreferenceForKey() and widget.preferenceForKey() to save and then restore data for the application.

Source file: StoreSettings.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="script/StoreSettings.js" />
<title>StoreSettings</title>
</head>
<body onload="init( )">
<div>
<input id="txt" type="text"></input>
<input type='button' onclick='dochange( )' value='Save'/>
</div>
</body>
</html>

Source file: StoreSettings.js

/* 
* This function loads previously saved string and sets it to input field
*/

function init( ) {
// Creating new menu item
var save = new MenuItem("Save", 12);
 
// Setting handlers for main menu items
save.onSelect = dochange;
 
// Adding items to menu
menu.append(save);
 
// Start displaying menu
menu.showSoftkeys();
 
// Checking existence of key and loadig string to input field if exists
if(widget.preferenceForKey('key'))
{
document.getElementById( 'txt' ).value = widget.preferenceForKey( 'key' );
}
}
 
/**
* Function saves string from input field to system
*/

function dochange( ) {
widget.setPreferenceForKey( document.getElementById( 'txt' ).value, "key" );
}


Postconditions

Upon loading shows text input field. You can change text in this field and use "Save" button or menu command to save it. Saved text will be restored to text input field on next launch of the application.

Supplementary material

  • You can test the storing of setting in action in a simple, executable application into which this code snippet has been patched. The executables and source files are available for download at: Media:Storing_settings_in_WRT.zip.
  • You can examine all the changes that are required to implement the above mentioned features in an application. The changes are provided in unified diff and color-coded diff formats: link_to_patchs
96 page views in the last 30 days.