4

I have a DialogView which stores settings in shared preferences. It is located in package A and i have another activity which is located in package B, which should be able to read these preferences.

So I created a wrapper class, which takes context and shared preference name and retrive these settings. When shared preferences are set at the first time everything works great, but when I change it, I got the same result, which was set at first time.

Problem is I save preference in one process and need to be able to read them in another.

So it seems like Context has changed and I am not able to retrive new context. What should I do to get up to-date shared preference?

Thank you on advance.

Please take a look at my wrapper class

public class PhotoAppWidgetSettingsProxy extends Proxy {

    private final static String PREFERENCES_NAME = PhotoAppWidgetSettingsProxy.class.getName();
    private final static int PREFERENCES_MODE = Context.MODE_PRIVATE;

    private Context mCtx = null;
    private SharedPreferences pref = null;
    private SharedPreferences.Editor editor = null;


    public PhotoAppWidgetSettingsProxy(String name, Context context) {
        super(name, context);       
        mCtx = context;
        pref = context.getSharedPreferences(PREFERENCES_NAME, PREFERENCES_MODE);
        editor = pref.edit();
    }


    private final static String FRAME = "FRAME";

    /**
     * Sets selected frame mode 
     * @param frame id
     */
    public void setFrameMode(int frameId){
        editor.putInt(FRAME, frameId);

        Log.d(PREFERENCES_NAME, "SET MODE="+frameId);
        boolean success = editor.commit();
        Log.d(PREFERENCES_NAME, "SET MODE="+success);
    }
    /**
     * Gets selected frame mode 
     * @return frame id
     */
    public int getFrameMode(){
        Log.d(PREFERENCES_NAME, "GET MODE="+pref.getInt(FRAME, 0));
        return pref.getInt(FRAME, 0);
    }

SOLVED:

private final static int PREFERENCES_MODE = Context.MODE_MULTI_PROCESS;
unresolved_external
  • 1,930
  • 5
  • 30
  • 65

2 Answers2

3
private final static int PREFERENCES_MODE = Context.MODE_MULTI_PROCESS;
unresolved_external
  • 1,930
  • 5
  • 30
  • 65
2

When accessing shared preferences/values, I have found it useful to write a CustomApplication class extending Application. I can place any necessary fields/methods in there, and easily acquire them from any of the other Android classes by using:

CustomApplication app = (CustomApplication) getApplication(); 
int x = app.getX(); 

Does that help you at all?

breadbin
  • 584
  • 1
  • 11
  • 19