Android : How to wait until Timer and Timertask finish their work on UI Thread -
i have fragment. in fragment, have timer run timertask periodically. override onpause
when switch fragment, timertask , timer stop work.
@override public void onpause() { super.onpause(); timertask.cancel(); timer.cancel(); timer.purge(); }
and here code switching fragment :
orderfragment fragment2 = new orderfragment(); fragment2.setarguments(arguments); getsupportfragmentmanager().begintransaction().replace(r.id.item_detail_container, fragment2).commit();
but when switch different fragment, receive null point exception
because timertask cannot access ui thread anymore (because have used runonuithread
)
so, question : how sure timer , timertask stop work, before can change fragment.
thanks :)
it seems onhiddenchanged(boolean hidden) need. fragment
onpause()
documentation:
this tied activity.onpause of containing activity's lifecycle.
so onpause()
isn't called when fragment no longer visible. move timer canceling code onhiddenchanged()
.
Comments
Post a Comment