Akhil Mohammed
Akhil Mohammed

Reputation: 63

Date Picker, How do I restore the previous selected Date value when cancel is pressed

I have a text view where date is set using a date picker. The text view is editable only when edit button is pressed and a layout with two buttons Done and Cancel are made visible. The date is only to be set if the user presses Done and if he presses cancel, the previous date should be restored.

How do i do this?enter image description here

The reason why i'm doing this is because i'm planning to add many more editable fields and on press of cancel, all values should go back to the previous.

Upvotes: 1

Views: 167

Answers (1)

Tanim reja
Tanim reja

Reputation: 2188

This is just an idea. You can use SharedPreference..

if user press cancel button then you need to set previous data from sharedpreference and if user press done button then of course you need to update your sharedpreference..

like this: for update sp on Done button press

 SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("date1",textView.getText().toString());
editor.commit();

for Cancel button press: get the previous value from sp

String date1=sharedPreferences.getString("date1",null);
textView.setText(date1);

Above is just a example.. Hope it helps you.. Thank You

Upvotes: 1

Related Questions