- Notifications
You must be signed in to change notification settings - Fork288
Memory safer implementation of android.os.Handler
License
badoo/android-weak-handler
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Memory safer implementation of android.os.Handler
Original implementation of Handler always keeps hard reference to handler in queue of execution.Any object in Message or Runnable posted toandroid.os.Handler
will be hard referenced for some time.If you create anonymous Runnable and call topostDelayed
with large timeout, that Runnable will be heldin 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.
You can read moreon our blog post.
WeakHandler
is trickier thanandroid.os.Handler
, it will keepWeakReferences
to runnables and messages,and GC could collect them onceWeakHandler
instance is not referenced any more.
Add JitPack repository to your build.gradle:
repositories { maven { url'https://jitpack.io' }}dependencies { implementation'com.github.badoo:android-weak-handler:1.3'}
Use WeakHandler as you normally would use Handler
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); }}
Weak Handler is brought to you byBadoo Trading Limited and it is released under theMIT License.
Read more on ourtech blog or explore our otheropen source projects
About
Memory safer implementation of android.os.Handler