tags:

views:

57

answers:

3

Hello, i have a user preference in my app, which gets ued by different activity. I would like to know the best way to utilise those preferences between different activities in my App.

I have this idea to create a shared preference object from the main activity and from there send intents to the different activities to take actions. would it work?.. or just keep calling getsharedpreferences() from every activity?. Thank you.

A: 

You can use this way and declare same variables with same name in all activites where you want to use.

  public static final String PREFS_NAME = "MyPrefsFile";
  static SharedPreferences settings;
  SharedPreferences.Editor editor;
  int wordCount;

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = getSharedPreferences(PREFS_NAME, 0);
    editor = settings.edit();

    wordCount = settings.getInt("wordCount", 4); 

  }

Here initially wordCount will give 4; And when you edit wordCount and want to store again

  editor.putInt("wordCount", 6);
  editor.commit();

You have to declare this same variables in activities where you want to use shared preferences. And its better you call getSharedPreferences in every activity.

I don't think that passing that preference in intent will work.

krunal shah
A: 

You can of course use shared preferences in your applications.

If you have more than a simple type than string or int, you can use a singleton or extends the application class which will be accessible by all activities of your application. => No disk access here. Simply kept in memory.

William Remacle
A: 

Sending shared preferences through intents seems overcomplicated. You could wrap the shared preferences with something like the below and call the methods directly from your activities:

public class Prefs {
    private static String MY_STRING_PREF = "mystringpref";
    private static String MY_INT_PREF = "myintpref";

    private static SharedPreferences getPrefs(Context context) {
        return context.getSharedPreferences("myprefs", 0);
    }

    public static String getMyStringPref(Context context) {
        return getPrefs(context).getString(MY_STRING_PREF, "default");
    }

    public static int getMyIntPref(Context context) {
        return getPrefs(context).getInt(MY_INT_PREF, 42);
    }

    public static void setMyStringPref(Context context, String value) {
        // perform validation etc..
        getPrefs(context).edit().putString(MY_STRING_PREF, value).commit();
    }

    public static void setMyIntPref(Context context, int value) {
        // perform validation etc..
        getPrefs(context).edit().putInt(MY_INT_PREF, value).commit();
    }
}
fornwall
hmm.. too many choices here. but actually my user preference contains checkboxes that performs action in a listview. so i might go with the suggestions here. only problem is that its populating from a customCursorAdapter, so jjst figuring out where to call it
manuelJ
yeah.. it works, would have really shot myself in the foot if i had gone through the intent route!
manuelJ