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 ?