Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Memory safer implementation of android.os.Handler

License

NotificationsYou must be signed in to change notification settings

badoo/android-weak-handler

Repository files navigation

Memory safer implementation of android.os.Handler

Problem

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.

Solution

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.

Screenshot

Usage

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);    }}

Credits

Weak Handler is brought to you byBadoo Trading Limited and it is released under theMIT License.

Blog

Read more on ourtech blog or explore our otheropen source projects

About

Memory safer implementation of android.os.Handler

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp