Friday, 18 January 2013

SharedPreferences in Android(1)

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

Data storage options in Android:

  1. SharedPreferences: Store private primitive data in key-value pairs.
  2. Internal Storage: Store private data on the device memory.
  3. External Storage: Store public data on shared external storage.
  4. SQLiteDatabase: Store structured data in private database.
  5. Network Connection: Store data on the web with your own network server.
SharedPreferences: The SharedPreferences class provides general framework that allows you to save  and retreive persistent key-value pairs of primitive datatypes .

you can use SharedPreferences to save any primitive data : booleans, floats, ints, longs, Strings. The data will persist across user sessions (even your application kills).

To get SharedPrferences object for your application use the following method
getSharedPrefeneces()

Ex: SharedPreferences sp = getSharedPrefereces(getApplicationContext(),0);


-->call edit() to get a SharedPreferences.Editor to get writing permissions to store the data in SharedPreferences.

-->Add values with methods such as putBoolean(), putString(), putInt(), putLong() and putFloat()

-->commit the new values with commit()

To read values: To read values , use SharedPreferences methods such as getBoolean(), getString(), getInt(), getFloat() and getLong()

Example Application:

ScreenShots:



activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:ems="10"
        android:inputType="textPersonName" >
    </EditText>

</RelativeLayout>

MainActivity.java:
package com.ram.sharedprefs1;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
String PREFS = "prefs";
EditText edit;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "onCreate()", Toast.LENGTH_LONG).show();
 edit = (EditText)findViewById(R.id.editText1);
 
 sp = getSharedPreferences(PREFS, 0);

String name = sp.getString("myname", "");
edit.setText(name);
}

@Override
protected void onDestroy() {
 
Toast.makeText(getApplicationContext(), "onDestroy()", Toast.LENGTH_LONG).show();

String name = edit.getText().toString();
sp = getSharedPreferences(PREFS, 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("myname",  name);
editor.commit();
super.onDestroy();
}

}