android - How to exit all the Async task on cancellation of activity? -
according post below: http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html. says that:
in such cases, application not shutdown, foreground tasks have been closed or changed, background tasks need know of , try exit gracefully
to achieve this, calling cancel
function on asynctask
instances. right way? in image lazy loading, don't keep track of asynctask
s alive (and fetching images), how android os
cancel them too?
you can cancel asynctask
checking thread(asynctask) object's status.
private example ex = new example(); class example asynctask<object, void, void> { @override protected void doinbackground(object... params) { string result = null; if(!iscancelled()) result = gethttprestmanager().send(); if(!iscancelled()) { // codes } ... return null; } } public boolean cancel() { switch(ex.getstatus()) { case running: case pending: return ex.cancel(true); case finished: return false; } return false; }
after cancel thread, it's status returns running
or finished
. if status not pending
, cannot execute thread. have initialize new thread object ex = new example()
before every .execute()
.
Comments
Post a Comment