Avg. Rating 4.8

Problem

I want a simple way to save some data in my mobile application.

Solution

Using the PersistenceManager I can easily save key/value pairs of data in my mobile application.

Detailed explanation

The PersistenceManager provides a simple method to save data to a Local Shared Object.

I create an instance of the PersistenceManager and use it's setProperty and getProperty methods to save and retrieve data. The following View has a TextInput which is used to enter some text, then a save button uses the PersistenceManager to save that text. The when the view is added again the data is loaded from the PersistenceManager and displayed in the TextInput. There is also a clear button that clears any saved data.

        <?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
title="HomeView"
add="addHandler(event)">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;

import spark.managers.PersistenceManager;
protected function saveButton_clickHandler(event:MouseEvent):void
{
var saveManager:PersistenceManager = new PersistenceManager();
saveManager.setProperty("myText", myInput.text);
}

protected function addHandler(event:FlexEvent):void
{
var loadManager:PersistenceManager = new PersistenceManager();
if(loadManager.load())
{
var savedData:Object = loadManager.getProperty("myText");
if(savedData)
myInput.text = savedData.toString();
}
}

protected function clearButton_clickHandler(event:MouseEvent):void
{
var persistenceManager:PersistenceManager = new PersistenceManager();
persistenceManager.clear();
myInput.text = "";
}

]]>
</fx:Script>
<s:TextInput width="100%" id="myInput"/>
<s:Group width="100%">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:Button id="saveButton" label="Save" click="saveButton_clickHandler(event)" width="100%"/>
<s:Button id="clearButton" label="Clear" click="clearButton_clickHandler(event)" width="100%"/>
</s:Group>
</s:View>
 
  
DataPersistence.zip
[Project file]

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes