Answer the question
In order to leave comments, you need to log in
Is Preferences accessible from another class?
Understanding data storage in Android. There was a problem:
Constants and global variables
SharedPreferences sharePref;
static final String SAVED_ANSWER = "saved_answer";
static final int MAX_ANSWER = 5;
sharePref = getPreferences(MODE_PRIVATE);
String savedText = sharePref.getString(Config.SAVED_ANSWER, "");
Log.d(TAG, "SAVED_ANSWER равно: " + savedText);
return savedText;
sharePref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor edit = sharePref.edit();
String savedText = sharePref.getString(Config.SAVED_ANSWER, "");
Log.d(TAG, "Сброс счетчика: он равен - " + savedText);
edit.putString(Config.SAVED_ANSWER, "0");
edit.apply();
Answer the question
In order to leave comments, you need to log in
Had the same problem. The classic SharedPreferences is only available in its activity. But getDefaultSharedPreferences can be used throughout the application. You can read more here:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question