I have TimePickerDialog appear when clicking on a Button and show the time I picked it on TextView. and when I leave the activity and come back textView is empty. I want to save the state of this textView
below is the code of TimePickerDialog.OnTimeSetListener
private TimePickerDialog.OnTimeSetListener TimePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hour, int minute) {
switch (timePickerInput) {
case R.id.set_sabah:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr = (ampm == 0) ? "AM" : "PM";
s1 = "Time is set in " + hour12format + " : " + minute + " " + ampmStr;
Toast.makeText(getApplicationContext(),s1 , Toast.LENGTH_LONG).show();
txtS.setText(s1);
break;
case R.id.set_masaa:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format2 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr2 = (ampm == 0) ? "AM" : "PM";
s2 = "Time is set in " + hour12format2 + " : " + minute + " " + ampmStr2;
Toast.makeText(getApplicationContext(),s2 , Toast.LENGTH_LONG).show();
txtM.setText(s2);
break;
}
setAlarm(mCalen);
}
};
I tried to savedInstanceState by this code below
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
savedInstanceState.putString("savedS1", txtS.getText().toString());
savedInstanceState.putString("savedS2", txtM.getText().toString());
and below is onRestoreInstanceState
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
String s1 = savedInstanceState.getString("savedS1");
String s2 = savedInstanceState.getString("savedS2");
txtS.setText(s1);
txtM.setText(s2);
}
I know that this solution is wrong but would appreciate any help.
Then if you want to save the state of a value after closing the activity, then you should go for shared preferences: http://developer.android.com/reference/android/content/SharedPreferences.html