0

I have a double data that I converted to String data. I am sending the string data to the another activity to be used in that activity. My challenge is that sharedpreferences is not sending the data to the second activity. I have added a toast in the first activity and it returns the value but does not pass the value to the second activity. First activity snippet

Double lat2 = 44.4; Double lng2 = 55.5;
        String lat22 = Double.toString(lat2);  String lng22 = Double.toString(lng2);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("latVal", lat22); editor.putString("lngVal", lng22);
            editor.commit();   
         Toast.makeText(this, "latitude main>>> " + lat22, Toast.LENGTH_SHORT).show();
         Toast.makeText(this, "longitude main>>> " + lng22, Toast.LENGTH_SHORT).show();

second activity snippets

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
         SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
         String latitude=sharedPref.getString("latVal", "");
         String longitude =sharedPref.getString("lngVal", "");
Toast.makeText(this, "latitude>>> " + latitude, Toast.LENGTH_SHORT).show();//returns nothing
         Toast.makeText(this, "longitude>>> " + longitude, Toast.LENGTH_SHORT).show();//returns nothing

please how can i successfully pass the shared data activity to the second activity

Float
  • 263
  • 2
  • 14
  • You're using two different `SharedPreferences`. The `Activity#getPreferences()` method returns a `SharedPreferences` that's "private" to that `Activity`. Instead, use `PreferenceManager.getDefaultSharedPreferences()` in both. Or use the `getSharedPreferences()` method with the same name in both. – Mike M. Mar 12 '17 at 07:27
  • problem solved.... – Float Mar 12 '17 at 07:39

0 Answers0