1

I am using Shared Preferences to store data which is came from EditText and set preferences data back to TextView but when i reopen my application textview shows default value. How can set changed data to the TextView and data should not be lost after reopening application. I tried onSaveInstanceState() and onSaveInstanceState() but this works when orientation change of application.

Here in my code i store data into shared Preferences and getting that data back to the TextView PRESET_MESSAGE_ONE i am storing value of EditText.

public void customDialogOne() {
    mDialog = new Dialog(_con);
    mDialog.setContentView(R.layout.custom_dialog_message);
    mDialog.getWindow().setBackgroundDrawableResource(R.color.black);
    mDialog.setTitle("Edit Preset Message");

    btnPresetDialogCancel = (Button) mDialog
            .findViewById(R.id.btnPrDialogCancel);
    edtPresetDialogMessage = (EditText) mDialog
            .findViewById(R.id.edtPrDialogMessage);

    btnPresetDialogSave = (Button) mDialog
            .findViewById(R.id.btnPrDialogSave);
    btnPresetDialogSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPref.writeString(SharedPref.PRESET_MESSGE_ONE,
                    edtPresetDialogMessage.getText().toString());
            msgOne = SharedPref.readString(SharedPref.PRESET_MESSGE_ONE);
            tm.showToast(msgOne);
            tvFrPresetMsgOne.setText(msgOne);
            mDialog.dismiss();

        }
    });
    btnPresetDialogCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mDialog.dismiss();
        }
    });
    mDialog.show();
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Reena
  • 1,368
  • 1
  • 11
  • 25
  • 1
    post your relevant code. – M D May 05 '14 at 05:38
  • Please post your code. So i will provide you the solution of this problm – user2323471 May 05 '14 at 05:40
  • you need to put logic of resetting this value in the on resume of the relevant activity. onSaveInstanceState is only for orientation changes – Lena Bru May 05 '14 at 05:41
  • Please let me know if any other way to achieve this task i am also looking for another solutions – Reena May 05 '14 at 05:50
  • you need to check it in `onResume()` method. – Piyush May 05 '14 at 05:53
  • @PiYusHGuPtA can you please explain what should i check in onResume()? – Reena May 05 '14 at 05:58
  • @Reena can you post `SharedPref.writeString()` and `SharedPref.readString()` method source code? –  May 05 '14 at 05:59
  • @Reena After save your data in `SharedPreference` and while you are retrieving data from you need to retrieve it in `onResume()` method. – Piyush May 05 '14 at 06:00
  • @Vandana Please have look at above two methods which i wrote in SharedPref static class // for String value public static void writeString(String key, String value) { getEditor().putString(key, value).commit(); } public static String readString(String key) { return getPreferences().getString(key, null); } – Reena May 05 '14 at 06:02
  • @Reena change this from `getPreferences().getString(key, null);` to `getPreferences().getString(key, "");` – Piyush May 05 '14 at 06:15
  • @Piyush Thanks but solution is not that much helpful, i able to get data using getString(key,null), i am using this method to get and read data from server and its working, i think issue is at only TextView which is not able to set shared Preferences value after reopening the application. – Reena May 05 '14 at 06:23
  • 1
    @Reena Yes it is. But you have to retrieve your data in `onResume()` method. means call `SharedPref.readString()` in `onResume()` method. – Piyush May 05 '14 at 06:24
  • Hi Friends my issue has resolved my sequence was wrong for storing value in shared preferences – Reena May 05 '14 at 09:37

2 Answers2

0

Have you commit your changes in the SharedPreference using SharedPref.commit(); ? Please check that one.

Sripathi
  • 1,760
  • 15
  • 20
  • public static void writeString(String key, String value) { getEditor().putString(key, value).commit(); } – Reena May 05 '14 at 09:14
0

My issue has resolved my sequence was wrong to store value in shared Preferences, I used following code:

btnPresetDialogSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            tvFrPresetMsgOne.setText(edtPresetDialogMessage.getText()
                    .toString());
            SharedPref.writeString(SharedPref.PRESET_MESSGE_ONE,
                    edtPresetDialogMessage.getText().toString());

            msgOne = SharedPref.readString(SharedPref.PRESET_MESSGE_ONE);
            tvFrPresetMsgOne.setText(msgOne);
            mDialog.dismiss();

        }
    });

and read SharePreferences string in onResume method()

Reena
  • 1,368
  • 1
  • 11
  • 25