3

I am trying to share a shared preference in between two activities of my project, but for some reason I am not able to pass the data.

I have Activity A which reads the shared preference and Activity B that reads as well as edit that shared preference.

Here is the code I am using to write the shared preference in Activity B:

SharedPreferences sharedPref = getSharedPreferences("myPrefs", Context.
    MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("theme", "black");
editor.commit();

and for reading in Activity A:

SharedPreferences sharedPref = getSharedPreferences("myPrefs", Context.
    MODE_WORLD_WRITEABLE);
String theme=sharedPref.getString("theme","blue");

I have tried using the different modes, and it worked in Activity B in PRIVATE mode but it wasn't shared to activity A. For some reasons I think I have two different shared preferences(same name) for the two different activities. How do I use the same shared preference for both the activities ?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Tanuj Wadhwa
  • 2,025
  • 9
  • 35
  • 57
  • MODE_APPEND is work perfect for me check my this answer:[http://stackoverflow.com/questions/21697172/android-how-to-save-user-name-and-password-after-the-app-is-closed/21697274#21697274](http://stackoverflow.com/questions/21697172/android-how-to-save-user-name-and-password-after-the-app-is-closed/21697274#21697274) – M D Mar 03 '14 at 03:59

3 Answers3

13

You can do simpler - in any activity:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

You will have the same prefs this way from anywhere.

http://developer.android.com/reference/android/preference/PreferenceManager.html#getDefaultSharedPreferences(android.content.Context)

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

To read shared datas in the second activity , change the mode :

from MODE_WORLD_WRITEABLE to MODE_WORLD_READABLE

 SharedPreferences sharedPref = getSharedPreferences("myPrefs",Context.MODE_WORLD_READABLE);
  String theme=sharedPref.getString("theme","blue");
Zied R.
  • 4,964
  • 2
  • 36
  • 67
0
// try this way
1. define SharedPreferences variable in SmartApplication class  also define read and write method SharedPreferences

private SharedPreferences sharedPreferences;

 @Override
 public void onCreate() {
        super.onCreate();

 sharedPreferences = getSharedPreferences("yourAppName", MODE_PRIVATE);

 } 

public void writeSharedPreferences(String key, String value) {
        SharedPreferences.Editor editor = readSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
}
// write Shared Preferences 
public void writeSharedPreferences(String key, boolean value) {
        SharedPreferences.Editor editor = readSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
}
// write Shared Preferences 
public void writeSharedPreferences(String key, float value) {
        SharedPreferences.Editor editor = readSharedPreferences().edit();
        editor.putFloat(key, value);
        editor.commit();
}

public void writeSharedPreferences(String key, int value) {
        SharedPreferences.Editor editor = readSharedPreferences().edit();
        editor.putInt(key, value);
        editor.commit();
}

// write Shared Preferences 
public void writeSharedPreferences(String key, long value) {
        SharedPreferences.Editor editor = readSharedPreferences().edit();
        editor.putLong(key, value);
        editor.commit();
}

// get Shared Preferences 
public SharedPreferences readSharedPreferences() {
        return sharedPreferences;
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67