Good day. I teach programming on my own. Help the guys figure out how to properly put the method into a separate class. There is a method in the main Activity that stores the String in the SharedPreferences settings:
public void menuTobe() { listStr = new Gson().toJson(new ArrayList<Integer>(Arrayl));// сохраняю Arrayl в строку SharedPreferences.Editor editor77 = mySettings1.edit(); editor77.putString(Tobe_ArrayList, listStr);// cохраняем Arrayl в строке в SharedPreferences editor77.apply(); Log.d("", "Сохраненный Tobe_ArrayList:" + Arrayl + listStr); if (mySettings1.contains(Tobe_ArrayList)) {// если сохранен Tobe_ArrayList SharedPreferences.Editor editor = mySettings1.edit(); editor.remove("Tobe_ArrayList"); // удаляем Tobe_ArrayList editor.apply(); } } Here I am trying to write it in a separate class.
public class SaveQuestions { // обьявление переменных SharedPreferences mySettings1; Activity a; String listStr; ArrayList<Integer> Arrayl; public SaveQuestions(Activity act) { a = act;// контекст через конструктор mySettings1 = act.getSharedPreferences(SPFILENAME, Context.MODE_PRIVATE); } public String menuTobe() { Arrayl = new ArrayList<>(); listStr = new Gson().toJson(new ArrayList<Integer>(Arrayl));// сохраняю Arrayl в строку SharedPreferences.Editor editor77 = mySettings1.edit(); editor77.putString(Tobe_ArrayList, listStr);// cохраняем Arrayl в строке в SharedPreferences editor77.apply(); Log.d("", "Сохраненный Tobe_ArrayList:" + Arrayl + listStr); if (mySettings1.contains(Tobe_ArrayList)) {// если сохранен Tobe_ArrayList SharedPreferences.Editor editor = mySettings1.edit(); editor.remove("Tobe_ArrayList"); // удаляем Tobe_ArrayList editor.apply(); } return listStr; } } I appeal to him in the main activit like this:
SaveQuestions sv = new SaveQuestions(this); sv. menuTobe(); In the main menuTobe() method saves the string to SharedPreferences , but not in a separate class.