android - PersistInt slowing down button response in custom preference -
i've created custom preference embeds 2 buttons (here have subclassed button fastbutton). problem executing persistint store preference drastically slows down response of button.
i had notion of executing persistint when preference's lifecycle ended, not find appropriate method override (i.e. there nothing onpause() preference class).
i unsuccessful @ trying use asynctask move persistint off of ui thread.
any suggestions how should go mitigating effect of persistint on ui response?
public final class stepperpreference extends preference { public int mcurrentvalue = 1; public int maxvalue = integer.max_value; public int minvalue = integer.min_value; private textview mtext; private fastbutton plusbutton; private fastbutton minusbutton; public stepperpreference(context context) { super(context); } public stepperpreference(context context, attributeset attrs) { super(context, attrs); parsecustomattributes(attrs); } public stepperpreference(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); parsecustomattributes(attrs); } public void setmcurrentvalue(int value) { if (mcurrentvalue != value) { mcurrentvalue = value; persistint(mcurrentvalue); } } private void parsecustomattributes(attributeset attrs) { int maxvalueattrint=integer.max_value; int minvalueattrint=integer.min_value; if (attrs!=null) { typedarray a=getcontext() .obtainstyledattributes(attrs, r.styleable.stepperpreference, 0, 0); maxvalueattrint = a.getint(r.styleable.stepperpreference_maxvalue, integer.max_value); minvalueattrint = a.getint(r.styleable.stepperpreference_minvalue, integer.min_value); a.recycle(); } if (maxvalueattrint > minvalueattrint) { maxvalue = maxvalueattrint; minvalue = minvalueattrint; } } @override protected view oncreateview(viewgroup parent) { layoutinflater li = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service); view view = (view) li.inflate(r.layout.stepper_preference, parent, false); mtext = (textview) view.findviewbyid(r.id.text_view); context context = getcontext(); int localdefaultvalue = 0; mcurrentvalue = getpersistedint(localdefaultvalue); mtext.settext(integer.tostring(mcurrentvalue)); plusbutton = (fastbutton) view.findviewbyid(r.id.plus_button); plusbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (mcurrentvalue < maxvalue) { mcurrentvalue++; mtext.settext(integer.tostring(mcurrentvalue)); persistint(mcurrentvalue); } } }); minusbutton = (fastbutton) view.findviewbyid(r.id.minus_button); minusbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (mcurrentvalue > minvalue) { mcurrentvalue--; mtext.settext(integer.tostring(mcurrentvalue)); persistint(mcurrentvalue); } } }); return view; } @override protected object ongetdefaultvalue(typedarray a, int index) { int localdefaultvalue = 0; object result = a.getint(index, localdefaultvalue); return result; } @override protected void onsetinitialvalue(boolean restorevalue, object defaultvalue) { int localdefaultvalue = 0; setmcurrentvalue(restorevalue ? this.getpersistedint(localdefaultvalue) : (int) defaultvalue); } }
checkboxpreference
, via twostatepreference
superclass, uses persistboolean()
saving preference value, using persistint()
. not perceive significant latency in processing of checkbox
. means 1 of 2 things:
i'm troglodyte , incapable of seeing obvious delays in animations , such
checkboxpreference
not exhibit problems seeing instepperpreference
note: these 2 possibilities not mutually exclusive
if assume #2 correct, there's else afoot. method tracing, see spending time "downstream" persistint()
, may prove useful determining different stepperpreference
.
from comment, had listener responding preference changes, , causing sluggish response. "inline" preferences, checkboxpreference
, stepperpreference
, more "twitchy" dialogpreference
subclasses listpreference
, because takes less work change preference state (e.g., 1 screen tap versus 2+). result, listeners need cheaper. example, might hold off on doing significant work until user has left preferencefragment
, know preference values stable @ least second or so.
Comments
Post a Comment