DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Saving Users Preferences In Air

Jonnie Spratley user avatar by
Jonnie Spratley
·
Sep. 21, 08 · · Code Snippet
Like (0)
Save
Tweet
576 Views

Join the DZone community and get the full member experience.

Join For Free
For saving a users preferences in Air, use the code as follows:

			
import com.adobe.air.preferences.PreferenceChangeEvent;
import com.adobe.air.preferences.Preference;

/* Create a new prefernce variable and give it a name */
private var prefs:Preference =  new Preference( "settings.obj" );

/* Create a new bytearry to store the data */
private var byteArray:ByteArray = new ByteArray();

/* Create a new fileStream variable to open up the local file system */
private var fileStream:FileStream;

/* The user data */
[Bindable] private var userData:User;

/* Register some event listeners to see when the prefrences change */
private function init():void
{				
	prefs.addEventListener( PreferenceChangeEvent.PREFERENCE_CHANGED_EVENT, onPrefChange );
	checkSettings();
}

/* When they change, set the action to edit, and update the files */
private function onPrefChange( event:PreferenceChangeEvent ):void
{
	if ( event.action == PreferenceChangeEvent.ADD_EDIT_ACTION )
	{
		//The new value
		trace( event.newValue );
		
	} else if ( event.action == PreferenceChangeEvent.DELETE_ACTION )
	{
		//The old value
		trace( event.oldValue );
	}
	
}			

/* Save the settings, it the checkbox is selected then lets save it */
private function saveSettings():void
{
	if ( cb_remember.selected )
	{
		/* preferences variable, give it a name, set the value, encyrpted or not */
		prefs.setValue( "username", txt_username.text, false );
		prefs.setValue( "password", txt_password.text, true );				
		prefs.save();
	} else {
		//Do nothing
	}				
	//Saved or Edited preferences
	trace( prefs );
}		

/* 
When the application loads, check the settings to see if anything has changed, if it has, set the value
of the new values to the values of the inputs 
*/
private function checkSettings():void
{			
	prefs.load();				
	txt_username.text = prefs.getValue( "username" );
	txt_password.text = prefs.getValue( "password" );
}

//Clear settings, and start from scratch
private function clearSettings():void
{
	txt_username.text = "";
	txt_password.text = "";
}
			
AIR (program)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Use Cluster Mesh for Multi-Region Kubernetes Pod Communication
  • An Overview of 3 Java Embedded Databases
  • The End of the Beginning for Apache Cassandra
  • What Are Cookies in Servlets?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo