You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original implementation of Handler always keeps hard reference to handler in queue of execution.
Any object in Message or Runnable posted to android.os.Handler will be hard referenced for some time.
If you create anonymous Runnable and call to postDelayed with large timeout, that Runnable will be held
in memory until timeout passes. Even if your Runnable seems small, it indirectly references owner class,
which is usually something as big as Activity or Fragment.
WeakHandler is trickier than android.os.Handler , it will keep WeakReferences to runnables and messages,
and GC could collect them once WeakHandler instance is not referenced any more.
importcom.badoo.mobile.util.WeakHandler;
publicclassExampleActivityextendsActivity {
privateWeakHandlerhandler; // We still need at least one hard reference to WeakHandlerprotectedvoidonCreate(BundlesavedInstanceState) {
handler = newWeakHandler();
...
}
privatevoidonClick(Viewview) {
handler.postDelayed(newRunnable() {
view.setVisibility(View.INVISIBLE);
}, 5000);
}
}