0

I have problem with sharedpreferences, something goes wrong and I always get a default value. My sharedpref class is:

public class IntolleranceData {

    static SharedPreferences intolleranceData;
    static SharedPreferences.Editor intolleranceEditor;

    static final String FISH_KEY="00000";
}

I save value in Activity by:

intolleranceData = getApplicationContext().getSharedPreferences("intolleranceData", Context.MODE_PRIVATE);
intolleranceEditor = intolleranceData.edit();
intolleranceEditor.putString(FISH_KEY, "something").apply();
Toast.makeText(getApplicationContext(), "fish: " + intolleranceData.getString(FISH_KEY, "error"), Toast.LENGTH_LONG).show();

and this is great (toast shows correct string -"fish: something") but if I try to use sharedpreferences in fragment (in the same Activity and SharedPreferences, Activity and Fragment are in the same package) by:

intolleranceData = getActivity().getSharedPreferences("intolleranceData", Context.MODE_PRIVATE);
intolleranceEditor = intolleranceData.edit();

String myKey = intolleranceData.getString(FISH_KEY,"error");
Toast.makeText(getActivity(),"fish: "+myKey,Toast.LENGTH_LONG).show();

It shows "fish: 00000" so this is default value...

Is there any way to solve my problem?

barmi
  • 665
  • 6
  • 22
  • No need to call edit() when you read data.. only getSharedPreferences() then your getString() – Idan Jul 28 '16 at 20:02
  • I know that intolleranceEditor = intolleranceData.edit(); in fragment is useless but I only declare it and do nothing. I read data by intolleranceData.getString(FISH_KEY,"error"); but it doesn't work... – barmi Jul 28 '16 at 20:09

6 Answers6

1

Try checking to make sure that your GetActivity() isn't null before you reference it to return the SharedPreferences.

Also, you are using 'apply()' to add your changes to the SharedPreferences, which is a process which runs in the background. If you try and read from the data too early (e.g. simultaneously) then you can often get the default value. Alternatively, use .commit() to save your changes to the SharedPreferences.

Finally, if the problem is unrelated to either of these common issues, saving 'getActivity().getApplicationContext()' to a variable when the fragment is initialized, it should help with the problem of the context not linking correctly!

Best of Luck!

JFed8
  • 118
  • 9
  • I checked it and getActivity() doesn't return null – barmi Jul 28 '16 at 20:21
  • You are using 'apply()' to add your changes to the SharedPreferences, which is a process which runs in the background. If you try and read from the data too early (e.g. simultaneously) then you can often get the default value. Alternatively, use .commit() to save your changes to the SharedPreferences. – JFed8 Jul 28 '16 at 20:24
  • If your problem is not related to the speed of the update to SharedPreferences, I would recommend saving 'getActivity().getApplicationContext()' to a variable as the fragment is created, which should help you keep the context linked even when the fragment is detached. – JFed8 Jul 28 '16 at 20:27
  • Have you succeeded yet? I'm hoping you get this up and running perfectly as you would like it to! – JFed8 Jul 28 '16 at 23:00
  • code is not perfect but application works, when I put a few value into SharedPref only first was fine so I created one SharedPref object for one value – barmi Jul 28 '16 at 23:14
0

There is need to change your file like this, In the Place of

intolleranceData = getActivity().getSharedPreferences("intolleranceData", Context.MODE_PRIVATE);
intolleranceEditor = intolleranceData.edit();

String myKey = intolleranceData.getString(FISH_KEY,"error");
Toast.makeText(getActivity(),"fish: "+myKey,Toast.LENGTH_LONG).show();

You Must change Your code Like this, way,.... and also you just simply change your getActivity() with className.this

intolleranceData = MainActivity.this.getSharedPreferences("intolleranceData", Context.MODE_PRIVATE);
intolleranceEditor = intolleranceData.edit();

String myKey = intolleranceData.getString(FISH_KEY,"error");
introlleranceEditor.commit();
introllerenceEditor.apply();
Toast.makeText(getActivity(),"fish: "+myKey,Toast.LENGTH_LONG).show();
ballu
  • 49
  • 1
  • 12
  • I tried but there is an error Error:(54, 40) error: not an enclosing class: MainActivity (in MainActivity.this.getSharedPreferences) – barmi Jul 28 '16 at 20:24
  • you want to display data in fragment@barmi – ballu Jul 28 '16 at 20:34
  • There is need to implement **FragmentManager** and **FragmentContainer** in your activity – ballu Jul 28 '16 at 20:35
  • Try to change MainActivity.this.getSharedPreferences in both main and sharedpreferedes – ballu Jul 28 '16 at 20:38
  • This is a little more complicated, I have MainActivity, there is container for fragment and fragment has another container but I am pretty sure that there is not problem, everything is displaying correct, just sharepref doesn't works – barmi Jul 28 '16 at 20:40
  • this is impossible to use it in fragment (MainActivity.this.getSharedPreferences) – barmi Jul 28 '16 at 20:46
  • hoo, kk simply try this code with out giving **MainActivity.this** – ballu Jul 28 '16 at 20:58
0

You don't need editor to read data. When you send data use

intolleranceEditor.putString(FISH_KEY, "something");
intolleranceEditor.commit();
miepsik
  • 63
  • 8
0

You should commit after you add the string...

intolleranceEditor.putString(FISH_KEY, "something");
intolleranceEditor.commit();
Graham
  • 7,431
  • 18
  • 59
  • 84
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I used editor.putString(KEY,"str").commit; but tried your version too, still doesn't work – barmi Jul 28 '16 at 20:44
0

I got your Problem i will give you a Perfect answer for your problem simply check this code......

public class MainActivity extends AppCompatActivity {


    static SharedPreferences intolleranceData;
    static SharedPreferences.Editor intolleranceEditor;
    static final String FISH_KEY="00000";
static final String File_Name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

intolleranceData = MainActivity.this.getSharedPreferences("File_Name", Context.MODE_PRIVATE);
String myKey = intolleranceData.getString(FISH_KEY,"");
Toast.makeText(getActivity(),"fish: "+myKey,Toast.LENGTH_LONG).show();

   }
}

and in Shared Preferece or Inside button Click you can write this code

intolleranceData = MainActivity.this.getSharedPreferences("File_Name", Context.MODE_PRIVATE);
intolleranceEditor = intolleranceData.edit();
intolleranceEditor.putString(FISH_KEY, "something")
intolleranceEditor.commit();
intolleranceEditor.apply();
Toast.makeText(getApplicationContext(), "fish: " + intolleranceData.getString(FISH_KEY, "error"), Toast.LENGTH_LONG)..show();

If you Solve your problem give a simple response

ballu
  • 49
  • 1
  • 12
0

I understand that you trying to use this code display a message in the Fragment kk, try this code.....

public class MainActivity extends AppCompatActivity {


    static SharedPreferences intolleranceData;
    static SharedPreferences.Editor intolleranceEditor;
    static final String FISH_KEY="00000";
static final String File_Name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

intolleranceData = getActivity().getSharedPreferences("File_Name", Context.MODE_PRIVATE);
String myKey = intolleranceData.getString(FISH_KEY,"");
Toast.makeText(getActivity(),"fish: "+myKey,Toast.LENGTH_LONG).show();

   }
}

and In Shared Prefereces

intolleranceData = getApplicationContext().getSharedPreferences("File_Name", Context.MODE_PRIVATE);
intolleranceEditor = intolleranceData.edit();
intolleranceEditor.putString(FISH_KEY, "something")
intolleranceEditor.commit();
intolleranceEditor.apply();
Toast.makeText(getApplicationContext(), "fish: " + intolleranceData.getString(FISH_KEY, "error"), Toast.LENGTH_LONG)..show();
ballu
  • 49
  • 1
  • 12
  • getApplicationContext() works only in Activities, getActivities() works in fragment, your suggestion is not compiled – barmi Jul 28 '16 at 21:07
  • i totally confusing your question please will you possible post your total coding @barmi – ballu Jul 28 '16 at 21:10
  • Here is an example how to solve my problem, it works [link]http://stackoverflow.com/questions/5734721/android-shared-preferences – barmi Jul 28 '16 at 21:21