1

I am trying to pass the values from one activity to another using shared preference but am getting null value.In the first activity i printed values in console and its getting printed but in that i couldn't retrieve the values.please help me to come out of this error

First activity: values are passed

sharedpreferences.edit().putString("CHECKPASS","changepass").commit();
editor.putString("FOOD",food1);
editor.putString("PLACE",place1);
editor.putString("COLOR",colour1);
editor.putString("BUY",buy1);
editor.commit();

Second Activity: where I am retrieving

     Log.d("succ", "reached");
        String yourpass = sharedpreferences.getString("CHECKPASS","changepass");
        Log.d("succ", "yournext" + yourpass);

        if (yourpass.equals("changepass")) {
            {
                final String foodshared = sharedpreferences.getString("FOOD","NULL");
                Log.d("succ", "foodshared" + foodshared);

                final String colorshared = sharedpreferences.getString("COLOR", "NULL");
                Log.d("succ", "colorshared" + colorshared);

                final String buyshared = sharedpreferences.getString("BUY", "NULL");
                Log.d("succ", "buyshared" + buyshared);

                final String placeshared = sharedpreferences.getString("PLACE", "NULL");
                Log.d("succ", "placeshared" + placeshared);



}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
disha
  • 51
  • 1
  • 8
  • Are there any errors in the `logcat` too? –  Sep 02 '17 at 06:17
  • I'd try to change this `sharedpreferences.edit().putString("CHECKPASS","changepass").commit();` to simply `editor.putString("CHECKPASS","changepass");` – Phantômaxx Sep 02 '17 at 06:20
  • @Mandy8055 there are no errors in logcat. But the control doesn't go to (succ", "reached)in the second activity.what should i do. – disha Sep 02 '17 at 06:21
  • Okay!!Have you tried the solution mentioned by @ModularSynth? –  Sep 02 '17 at 06:22
  • why you not use intent to pass data ?? – KuLdip PaTel Sep 02 '17 at 06:23
  • At both screen, are you using same SharedPreferences name to get the object of SharedPreferences? – Arti Patel Sep 02 '17 at 06:26
  • @ModularSynth Its not coming..am getting null value – disha Sep 02 '17 at 06:29
  • Weird. It should work, if it works for the other values. OR it means that you are getting an **NPE**, because you didn't create an instance of `editor`. – Phantômaxx Sep 02 '17 at 06:31
  • 1
    Please give an [mcve]. **This does not mean to post your entire app.** Rather, you need to be sure to include all relevant classes, methods, and variable declarations and initializations. I should be able to copy/paste your code and then compile and run it without any changes and get the exact same behavior you are asking about. – Code-Apprentice Sep 02 '17 at 06:34
  • @KuLdipPaTel even i used putExtra values are not passed...getting null valuue – disha Sep 02 '17 at 08:08
  • @Code-Apprentice shared preference only these two modules.....and in the first activity i tried printing values in console it got printed.but values are not passing to another activity – disha Sep 02 '17 at 08:12
  • please check this ans https://stackoverflow.com/a/8748539/7229971 – KuLdip PaTel Sep 02 '17 at 08:20
  • Please read the link I gave above. – Code-Apprentice Sep 02 '17 at 15:24

2 Answers2

1

Use this code

//For writing data

SharedPreferences.Editor 
editor=getSharedPreferences("nameOFSharedPref",MODE_PRIVATE).edit();
editor.putString("CHECKPASS","changepass");
editor.putString("FOOD",food1);
editor.putString("PLACE",place1);
editor.putString("COLOR",colour1);
editor.putString("BUY",buy1);
editor.apply();

//For Reading Data

SharedPreferences sharedPreferences=getSharedPreferences("nameOFSharedPref",MODE_PRIVATE);
String foodshared = sharedPreferences.getString("FOOD","NULL");
String colorshared = sharedPreferences.getString("COLOR", "NULL");
final String buyshared = sharedPreferences.getString("BUY", "NULL");
final String placeshared = sharedPreferences.getString("PLACE", "NULL");

Also while writing data don't use editor.commit() instead use editor.apply() because it handles data in background.

Avinash Saran
  • 144
  • 1
  • 9
0

UPDATE:

in your first activity

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);//or MODE_PRIVATE
Editor editor = pref.edit();
editor.putString("CHECKPASS","changepass")
editor.putString("FOOD",food1);
editor.putString("PLACE",place1);
editor.putString("COLOR",colour1);
editor.putString("BUY",buy1);
editor.commit();

IN your secondActivity

 SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);

 final String foodshared = pref.getString("FOOD",null);
            Log.d("succ", "foodshared" + foodshared);

            final String colorshared = pref.getString("COLOR",null);
            Log.d("succ", "colorshared" + colorshared);

            final String buyshared = pref.getString("BUY",null);
            Log.d("succ", "buyshared" + buyshared);

            final String placeshared = pref.getString("PLACE",null);
            Log.d("succ", "placeshared" + placeshared);

edit:

Initialization

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
Editor editor = pref.edit();

STORE THE DATA

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string


editor.commit(); // commit changes

Retrieving Data

pref.getString("key_name", null); // getting String
pref.getBoolean("key_name", null); // getting boolean
jigar savaliya
  • 474
  • 1
  • 8
  • 21