android - Handler.postDelayed and orientaton change issues -
android - Handler.postDelayed and orientaton change issues -
i facing unusual issue not able solve. thing is, have activity , after pressing button alter visibility of layouts. after using handler
in order revert situation after 4 seconds, , set before.
everything works expected except when alter device orientation, if alter device orientation during process, views not restored, not sure problem :s.
here relevant code
private view mloginformview; private view mloginstatusview; private boolean mlogginin = false; @override protected void oncreate(bundle savedinstancestate) { log.d(tag, "oncreate"); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login_layout); mloginformview = findviewbyid(r.id.login_form); mloginstatusview = findviewbyid(r.id.login_status); if (savedinstancestate == null) { mlogginin = false; } else { mlogginin = savedinstancestate.getboolean(getstring(r.string.user_login_in),false); log.d(tag,"restoring mlogin in = " + mlogginin); } findviewbyid(r.id.sign_in_button).setonclicklistener( new view.onclicklistener() { @override public void onclick(view view) { attemptlogin(); } }); if(mlogginin) showprogress(true); }
now nowadays relevant functions
//....... public void attemptlogin() { showprogress(true); mmenu.finditem(r.id.started).setvisible(false); handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { runonuithread(new runnable() { @override public void run() { showprogress(false); log.d(tag,"mloggin value progress = " + mlogginin); mlogginin = false; } }); } },4000); }
and showprogress()
private void showprogress(final boolean show) { mlogginin = show; log.d(tag, "showprogress " + mlogginin); int shortanimtime = getresources().getinteger( android.r.integer.config_shortanimtime); mloginstatusview.setvisibility(view.visible); mloginstatusview.animate().setduration(shortanimtime) .alpha(show ? 1 : 0).setlistener(new animatorlisteneradapter() { @override public void onanimationend(animator animation) { mloginstatusview.setvisibility(show ? view.visible : view.gone); } }); mloginformview.setvisibility(view.visible); mloginformview.animate().setduration(shortanimtime).alpha(show ? 0 : 1) .setlistener(new animatorlisteneradapter() { @override public void onanimationend(animator animation) { mloginformview.setvisibility(show ? view.gone : view.visible); } }); }
the weird thing, when onrestoreinstancestate
called after postdelayed
code has been executed, mlogginin
shows previous value (true), if had no effect @ all.
thanks in advance
when activity
recreated because of configuration change, different object instance, embedded view hierarchy. handler
tight old object instance, , modifies old view hierarchy too. hence don't see anything.
don't this: leaks memory , might lead crashes.
solution:
when post runnable
, save runnable
instance , current timestamp in 2 references. if runnable
runs normally, set timestamp -1 (within runnable
code).
on onsaveinstancestate(bundle)
if timestamp different -1, retain across configuration changes , remove runnable callbacks handler handler.removecallbacks(runnable)
.
when recreate activity restore timestamp , calculate difference current time. post new runnable
same logic delay (if 0 or negative might want run runnable straight away instead of posting it). runnable
tight new instance of activity
.
android handler
Comments
Post a Comment