android - Saving the state of a button onSaveInstanceState -
ok, 1 tricky me. idea of time log ems. press button action taken, , logs time later use. managed figure out how save textview string. now, how on earth save logtime1.setenabled(false); on rotation or on leaving activity, restores disabled? have button beside allow edit unlock button. here code.
public class timelog extends activity{
boolean logtimedis1=true; button logtime1; string time1; textview ivtimestamp; int counter=0; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.time_log); logtime1 = (button) findviewbyid(r.id.logtime1); ivtimestamp = (textview) findviewbyid(r.id.ivtimestamp); if(savedinstancestate != null) logtime1.setenabled(savedinstancestate.getboolean("logtimedis1", true)); logtime1.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { // todo auto-generated method stub time1 = new simpledateformat("hh:mm", locale.us).format(new date()); ivtimestamp.settext(time1); logtime1.setenabled(false); logtimedis1 = false; return false; } }); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); counter++; } @override protected void onsaveinstancestate(bundle outstate) { // todo auto-generated method stub super.onsaveinstancestate(outstate); outstate.putstring("ivtimestamp", ivtimestamp.gettext().tostring()); outstate.putboolean("logtimedis1", logtimedis1); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { // todo auto-generated method stub super.onrestoreinstancestate(savedinstancestate); ivtimestamp.settext(savedinstancestate.getstring("ivtimestamp")); } }
so, in line
if(savedinstancestate != null) logtime1.setenabled(savedinstancestate.getboolean("logtmedis1", false));
you don't check if savedinstancestate contains logtmedis1. say, savedinstancestate not null, not have logtmedis1, you'll false , button locked. actually, don't have check in case, change false true, , should work correctly.
Comments
Post a Comment