android user defaults

Send Snippet To: Save this snippet to Code Collector Pro -- view all balzan's snippets
language: Java
license: Other

Code for Snippet:

                
//--Init
int myvar = 12;
 
 
//--SAVE Data 
 
//Context is the activity
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);  
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("var1", myvar);
editor.commit();
 
 
 
 
//depending on which context your on you might need this
SharedPreferences preferences = getApplicationContext().getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);  
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("var1", myvar);
editor.commit();
 
 
//--READ data       
myvar = preferences.getInt("var1", 0);  
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 350
Date Added: 2014-02-05 02:57:39
Last Modified: 2014-02-19 16:32:02

Web Analytics