Studyon Minte9.com
Android




Shared Preferences



Android provides several options for you to save persistent application data. The solution you choose 
depends on your specific needs, such as whether the data should be private to your application or 
accessible to other applications (and the user) and how much space your data requires.

Here is an example that saves a preference int.
Every time app closes the view number increases. On limit 3, the number is reset.

	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.content.SharedPreferences;
	import android.os.Bundle;
	import android.widget.Toast;

	public class MainActivity extends Activity {
		
	    public static final String PREFS_NAME = "MyPrefsFile";		
	    public Integer views = 0;
			
	    @Override
	    protected void onCreate(Bundle state) {
		super.onCreate(state);
		setContentView(R.layout.activity_main);
		
		//Restore preferences
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		
		//Get value from preferences 
		views = settings.getInt("appViews", 0); // if not exists is 0
			//getString(), getBoolean(), getInt()

		//Reset prefereneces
		if (views > 3) {
			settings.edit().clear().commit();
			views = 0;
		}
		
		//Value display
		Toast.makeText(getApplicationContext(), 
                        "Last appViews: " + views, Toast.LENGTH_SHORT).show();
	    }
	    
	    @Override
	    public void onStop(){
		super.onStop();
		
		//We need an Editor object to make preference changes
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("appViews", ++views);
		
		//Commit changes
		editor.commit();
	    }
	    
	}


http://developer.android.com/guide/topics/data/index.html