Postdelay a runnable but does not run after activity recycled by GC
mricefox opened this issue · 2 comments
mricefox commented
Some code in activity like this:
new WeakHandler().postDelayed(new Runnable() {
@Override
public void run() {
// do something
}
}, 10_000);
``
If the activity finished within 10 seconds, the runnable will not run.Is there any idea to run the runnable after activity destroyed?
P.S.
I found some reason in WeakHandler.java
final ChainedRef mRunnables = new ChainedRef(mLock, null);
//`mRunnables` recycled after activity destroyed so nothing keep reference to the following runnable
erikandre commented
Hi @mricefox,
This is the intended behaviour and the main reason the library was created (to allow for non-critical AsyncTasks to be executed without having to worry as much about memory leaks).
If you have some background operation that must complete even if the user leaves the current activity, then I'd recommend that you use a Service instead.
mricefox commented
@erikandre ,Thanks for your reply