0

In MainActivity.java I write

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

Then I create object of Editor to put data in it

Editor edit = pref.edit();

Then I put data

edit.putString("1","Hello");
edit.commit(); / edit.apply();

In Second.java, I get preferences:

SharedPreferences pref = getPreferences(0);

then I try to receive data like

pref.getString("1",null);

and set it to text of textview. But this does not work.

Also, how do I access Preferences and editor in other java classes properly? I cannot understand the concept.

Rob Meeuwisse
  • 2,847
  • 1
  • 17
  • 21

4 Answers4

0

Get the context of your activity

Context shrdContext= ActivityClass.getContextOfApplication();

Now pass the context to get your shared preferences in your another class

SharedPreferences myPrefs= PreferenceManager.getDefaultSharedPreferences(shrdContext);
BDRSuite
  • 1,594
  • 1
  • 10
  • 15
0

Try this way

public void saveToSharedPrefrence(Context context, String word) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor = sharedPreferences.edit();

    if (sharedPreferences.contains("history")) {

        preExistRemove(word, context);

    } else {
        editor.putString("history", word.trim());
        editor.commit();

    }

}

Check this for more details

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89
0

You are writing to and reading from different preference files. Use the same file and it should work.

To get an instance of SharedPreferences you do this:

1) In activity MainActivity:

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

2) In activitySecond:

SharedPreferences pref = 
    getPreferences(0);

The first form opens preference file "My_Pref", the second form opens a file named after your activity class, i.e.: "Second". So they are reading and writing in different files.

I always use this form to open a preferences file:

SharedPreferences prefs = 
    PreferenceManager.getDefaultSharedPreferences(this);
Rob Meeuwisse
  • 2,847
  • 1
  • 17
  • 21
0

You have 3 ways to access preferences for a Android application.

  • The first you used is SharedPreferences pref = getApplicationContext().getSharedPreferences("My_Pref" , 0); is the first one. With this you can read and write to a custom-named shared preference file. In your case the name of your file would be My_Pref. This one is useful if you want to have differents preferences differents domain as it allow you to create many shared preferences with differents names. (ex : preferences for timezone, preference for your user).

  • The second getPreferences(int) allow to access preferences for an Activity and is bind closely to the activity calling it. The file created is named using the Activity name. In your case Second.

  • The third method PreferenceManager.getDefaultSharedPreferences(Context) create a shared preference file like the first method, but this time the file is named using your application package name. This is the best methods to use shared preferences, if you intend to have only one shared preference file.

In your initial question you wrote a data in one file and tried to read in an other file, which result in an error. That's why like Rob Meeuwisse write you must use PreferenceManager.getDefaultSharedPreferences(Context)

Kowlown
  • 920
  • 10
  • 26