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.
PreferencesManager.getDefaultSharedPreferences(context);- Yuriy SPb ♦SharedPreferences.Editor[] editor = {sPreferences.edit()};- Yuriy SPb ♦final SharedPreferences.Editor[] editor = {sPreferences.edit()};? If changed toSharedPreferences.Editor editor = sPreferences.edit();andeditor.putString();, there is no more error? - Ksenia