This question has already been answered:

In the application, I invoke a dialog in one activity, where I ask a question, based on the answer, I enter the data into SharedPreferences. After this, I immediately check whether the data was entered or not, shows what was entered.

//Метод показа диалога пункта "Аутенфикации" private void showAutenficationDialog() { final SharedPreferences.Editor[] editor = {sPreferences.edit()}; Log.d(Constants.LOG_TAG, "MainActivity - showAutenficationDialog - Метод показа диалога пункта \"Аутенфикации\""); //Создаем билдер AlertDialog.Builder builder = new AlertDialog.Builder(this); //Устанавливаем Заголовок builder.setTitle(R.string.dialog_autenfocation_titlle); builder.setMessage(R.string.dialog_autenfocation_text); builder.setPositiveButton(R.string.dialog_autenfocation_btn_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //объект для редактирования настроек editor[0].putString(Constants.AUTENT_ACTIVITY, "Да"); editor[0].commit(); Log.d(Constants.LOG_TAG, "MainActivity - showAutenficationDialog - Метод показа диалога пункта \"Аутенфикации\" - Выбрали Да"); Toast.makeText(MainActivity.this, R.string.toast_enter_pass_on, Toast.LENGTH_LONG).show(); //проверяем что сейчас в настройках, получилось внести или нет sPreferences = getPreferences(MODE_PRIVATE); //Если в настройках указано false - не проверяем пароль, открываем Main String flagProverka = sPreferences.getString(Constants.AUTENT_ACTIVITY, "---"); Log.d(Constants.LOG_TAG, " в настройках указано - " + flagProverka); } }); builder.setNegativeButton(R.string.dialog_autenfocation_btn_no, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //объект для редактирования настроек editor[0].putString(Constants.AUTENT_ACTIVITY, "Нет"); editor[0].commit(); Log.d(Constants.LOG_TAG, "MainActivity - showAutenficationDialog - Метод показа диалога пункта \"Аутенфикации\" - Выбрали Нет"); Toast.makeText(MainActivity.this, R.string.toast_enter_pass_off, Toast.LENGTH_LONG).show(); sPreferences = getPreferences(MODE_PRIVATE); //Если в настройках указано false - не проверяем пароль, открываем Main String flagProverka = sPreferences.getString(Constants.AUTENT_ACTIVITY, "---"); Log.d(Constants.LOG_TAG, " в настройках указано - " + flagProverka); } }); builder.show(); } 

In another activity, I need to get into this data:

 SharedPreferences sPreferences = getPreferences(MODE_PRIVATE); //Если в настройках указано false - не проверяем пароль, открываем Main String flagProverka = sPreferences.getString(Constants.AUTENT_ACTIVITY, "---"); Log.d(Constants.LOG_TAG, " в настройках указано - " + flagProverka); 

But I get the default value "---". Why is it so, why does the check immediately find the data, and in the other there is no activation. The feeling that I'm not writing down there.

Reported as a duplicate by temq participants, Yuriy SPb android Jan 23 '17 at 11:30 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Try to get global preferences like this: PreferencesManager.getDefaultSharedPreferences(context); - Yuriy SPb
  • And specify in what language you write - something is not quite obvious. For example, this is not at all clear what it is: SharedPreferences.Editor[] editor = {sPreferences.edit()}; - Yuriy SPb
  • And why do you need an array here: final SharedPreferences.Editor[] editor = {sPreferences.edit()}; ? If changed to SharedPreferences.Editor editor = sPreferences.edit(); and editor.putString(); , there is no more error? - Ksenia
  • Before using any method I advise you to read the documentation on it. - temq
  • This is 'SharedPreferences.Editor [] editor = {sPreferences.edit ()};' wants the Android studio during the dialogue, does not fundamentally change anything. - Eugene Zaychenko

1 answer 1

In short, I figured it out myself, if I use

 sPreferences = getPreferences(MODE_PRIVATE); 

A file is created with the name of the package + the name Activiti, that is, for each Activiti its own settings file, if you want the settings to be common for the entire application, you need to specify the file name in this way:

 sPreferences = getSharedPreferences("MyPref", MODE_PRIVATE); 

Then the settings are visible to all activations.