barmi
barmi

Reputation: 665

Android Sharedpreferences doesn't work

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?

Upvotes: 0

Views: 3052

Answers (6)

You should commit after you add the string...

intolleranceEditor.putString(FISH_KEY, "something");
intolleranceEditor.commit();

Upvotes: 0

ballu
ballu

Reputation: 49

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();

Upvotes: 0

ballu
ballu

Reputation: 49

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

Upvotes: 0

JFed
JFed

Reputation: 128

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!

Upvotes: 1

miepsik
miepsik

Reputation: 73

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

intolleranceEditor.putString(FISH_KEY, "something");
intolleranceEditor.commit();

Upvotes: 0

ballu
ballu

Reputation: 49

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();

Upvotes: 0

Related Questions