There is a method where I save the values ​​of the clicked item in my recyclerView:

private void saveFavorites(int position) { String name = personCategories.get(position); SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString(Constants.KEY_SP, name); editor.apply(); } 

Then I receive the data in another fragment as follows:

  private String getFavorites() { SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE); return sp.getString(Constants.KEY_SP, null); } 

The hitch is that I only get one item that I clicked last on the list. How to make it appear as an array? And all the elements that I clicked were added to the second fragment?

  • And what prevents a single class from creating a singleton that will store this data, and any other fragment or adapter, or anything else can refer to it, and inside to store any collections? - AZ
  • To be honest, a little incomprehensible, I'm a beginner. Can I have an example? or at least a passage / start to start. - Inkognito

4 answers 4

As an option, try to convert your strings and save them to json in the saveF Favorites method using the gson library.

After that, get your JSON strings in the getFQ method.

A good example is described here . Hope to help.

  • uh, and in more detail how can you please?) - Inkognito
  • @Inkognito added an example. - Morozov
  • Thank you, a good example, there are some irregularities there, but on the whole it has become vapid. - Inkognito
  • Just do not pull the whole GSON into the project for the sake of the string array, it’s like a cannon on a sparrow. - Eugene Krivenja
  • @EugeneKrivenja is very intently) this is a serious "disagreement". And you can be more constructive, why such an example doesn't suit you? It would be interesting to see a more weighty remark. - Morozov

Alternatively, use getStringSet/putStringSet .
In the first fragment, the code becomes more complicated, it will be necessary to read, add and rewrite a set of rows.

  • Thank you, I tried a similar option, well, a little not exactly what I asked, maybe I didn’t figure it out. - Inkognito

Only the last value is saved, since you save all the values ​​with one key and, accordingly, the subsequent record overwrites the previous one.

As a (bad) option, you can save a list of values, not just one value.

But, generally speaking, SharedPreferences not designed to store this kind of data, which you have already spoken about here many times.

  • this kind of what? I do not see what the author is going to store in the question. Most likely simple strings with names. - Morozov
  • This kind of @Morozov is some sort of intermediate position value for the purpose of transfer between components. Preferences are designed to store settings and other data of this kind, but not for communication, Android has more suitable tools and patterns for this. - pavlofff
  • @pavlofff agrees in principle, but if this goes for informational purposes, well, by “communication” do you mean “skipping” data on fragments? - Morozov

public class DataManager {// example of singleton

 private static List<Data> mDataList; private static SettingsDataManager sInstance; private DataManager() { } public static DataManager getInstance() { if (sInstance == null) { synchronized (DataManager.class) { mInstance = new DataManager(); } } return sInstance; } public void saveData(List<Data> data) { mDataList = data; } public List<Data> restoreData() { return mDataList; } 

}