1

I'm building my first multi-activity app. I have some geographic coordinates in Activity1 that are defined by the user and are saved to SharedPreferences:

// these strings are used when saving the users' preferred location
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";

private static final String TAG = "Activity1";
// actually saves the coordinates to the preferences 
private void saveCoordinatesInPreferences(float latitude, float longitude) {
    SharedPreferences prefs = 
       this.getSharedPreferences(getClass().getSimpleName(),
                       Context.MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putFloat(POINT_LATITUDE_KEY, latitude);
    prefsEditor.putFloat(POINT_LONGITUDE_KEY, longitude);
    //Log.i(TAG, "latitude is: " + latitude);
    //Log.i(TAG, "longitude is: " + longitude);
    prefsEditor.commit();
}

These SharedPreferences coordinates then need to be used by Activity2. I'm having trouble retrieving them. Here's a method I have written for retrieval. My variable latitude is not written to the log.

private static final String TAG = "Activity2";

protected void getLatLongPref() {
// adapted from http://mrbool.com/android-shared-preferences-managing-files-using-internal-external-storage/30508
// accessed April 10, 2015
    SharedPreferences pref = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY", MODE_PRIVATE);
    float latitudeUser = pref.getFloat("POINT_LATITUDE_KEY", 0); // getting users chosen latitude   
    Log.i(TAG, "latitude is: " + latitudeUser);

}

What do you think I'm doing wrong ?

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
mark
  • 537
  • 6
  • 25
  • Try using `this.getDefaultSharedPreferences()` in both of your Activities. Also what is the `POINT_LATITUDE_KEY` String in your first code snippet ? Also, see [here](http://stackoverflow.com/a/6714947/4428462) for how to view what actually gets put into the SharedPrefs (no need to parse xml). – Jonas Czech Apr 10 '15 at 17:31
  • @JonasCz ah sorry, `POINT_LATITUDE_KEY` and `POINT_LONGITUDE_KEY` are keys I've defined to be added to prefs. I've edited my question – mark Apr 10 '15 at 17:35
  • @JonasCz `this.getDefaultSharedPreferences()` is not available and the DDMS folder mentioned in you link is empty – mark Apr 10 '15 at 18:25

2 Answers2

1

You have used wrong context and preference name for both sharedpreferences. Change the first one to this:

SharedPreferences prefs = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY",
                       Context.MODE_PRIVATE);
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • This works. You just beat @JonasCz to it! Thanks both. So in summary i'm creating 2 `pref` objects : `SharedPreferences prefLat = this.getSharedPreferences("POINT_LATITUDE_KEY", Context.MODE_PRIVATE); SharedPreferences prefLong = this.getSharedPreferences("POINT_LONGITUDE_KEY", Context.MODE_PRIVATE);` – mark Apr 10 '15 at 19:43
  • @mark, You should use in both the cases samepreference name while putting the data and getting the data. – Android Killer Apr 11 '15 at 16:33
0

Change your first code snippet like this:

private void saveCoordinatesInPreferences(float latitude, float longitude) {
    SharedPreferences prefs = 
       this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
    [...]
}

...And your second one like this:

protected void getLatLongPref() {
    SharedPreferences pref = 
       this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
    [...]
}

Basically, It has to be the same in both cases. See this also.

Community
  • 1
  • 1
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65