Tuesday, January 25, 2011

Use getSharedPreferences() to retrieve a preferences object shared across multiple activity

Last exercise "Example of using SharedPreferences.Editor" get SharedPreferences object using the method getPreferences(), it allow accessing preferences associated with the activity. To use preferences that are shared across multiple application components (activities, receivers, services, providers), you can use the underlying Context.getSharedPreferences() method to retrieve a preferences object stored under a specific name.

Use getSharedPreferences() to retrieve a preferences object to shared across multiple activity

In this exercise, SharedPreferences is saved in main activity, it can be retrieved in the second activity.

Main activity, AndroidSharedPreferencesEditor.java
package com.exercise.AndroidSharedPreferencesEditor;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidSharedPreferencesEditor extends Activity {
 
 EditText editText1, editText2;
 TextView textSavedMem1, textSavedMem2;
 Button buttonSaveMem1, buttonSaveMem2;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
      
       textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
       textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
       editText1 = (EditText)findViewById(R.id.edittext1);
       editText2 = (EditText)findViewById(R.id.edittext2);
       buttonSaveMem1 = (Button)findViewById(R.id.save_mem1);
       buttonSaveMem2 = (Button)findViewById(R.id.save_mem2);
      
       buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);
       buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener);
      
       Button buttonStartAnother = (Button)findViewById(R.id.startanother);
       buttonStartAnother.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
             intent.setClass(AndroidSharedPreferencesEditor.this, another.class);
             startActivity(intent);
   }});
      
       LoadPreferences();
   }
  
  
  
   Button.OnClickListener buttonSaveMem1OnClickListener
    = new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SavePreferences("MEM1", editText1.getText().toString());
    LoadPreferences();
   }
    
   };
  
   Button.OnClickListener buttonSaveMem2OnClickListener
  = new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SavePreferences("MEM2", editText2.getText().toString());
    LoadPreferences();
   }
 
   };
  
   private void SavePreferences(String key, String value){
    SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }
  
   private void LoadPreferences(){
    SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String strSavedMem1 = sharedPreferences.getString("MEM1", "");
    String strSavedMem2 = sharedPreferences.getString("MEM2", "");
    textSavedMem1.setText(strSavedMem1);
    textSavedMem2.setText(strSavedMem2);
   }
}


layout for main activity, main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Saved Mem 1:"
   />
<TextView 
   android:id="@+id/savedmem1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Saved Mem 2:"
   />
<TextView 
   android:id="@+id/savedmem2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<EditText
   android:id="@+id/edittext1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button
   android:id="@+id/save_mem1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Save Mem 1"
   />
<EditText
   android:id="@+id/edittext2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button
   android:id="@+id/save_mem2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Save Mem 2"
   />
<Button
   android:id="@+id/startanother"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Start Another"
   />
</LinearLayout>


The second activity, another.java
package com.exercise.AndroidSharedPreferencesEditor;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class another extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.anothermain);
  
  TextView textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
       TextView textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
      
       SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String strSavedMem1 = sharedPreferences.getString("MEM1", "");
    String strSavedMem2 = sharedPreferences.getString("MEM2", "");
    textSavedMem1.setText(strSavedMem1);
    textSavedMem2.setText(strSavedMem2);
    
    Button buttonFinish = (Button)findViewById(R.id.finish);
    buttonFinish.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    finish();
   }});       
 }
}


layout for the second activity, anothermain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="The Another Activity"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Saved Mem 1:"
   />
<TextView 
   android:id="@+id/savedmem1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Saved Mem 2:"
   />
<TextView 
   android:id="@+id/savedmem2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />

<Button
   android:id="@+id/finish"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Finish"
   />
</LinearLayout>


Modify AndroidManifest.xml to incude another.java.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.exercise.AndroidSharedPreferencesEditor"
     android:versionCode="1"
     android:versionName="1.0">
   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".AndroidSharedPreferencesEditor"
                 android:label="@string/app_name">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
  <activity android:name=".another" />
   </application>
   <uses-sdk android:minSdkVersion="4" />

</manifest>


Download the files.

Related:
- SharedPreferences.Editor for RadioButton in RadioGroup


2 comments:

harireddy said...

hi,
this is very useful. Thanks a lot...
really very helpful.
Thanks ,
Hariprasad.

Anonymous said...

Thanks!