{"id":3606,"url":"https://github.com/badoo/android-weak-handler","last_synced_at":"2025-05-15T15:02:45.899Z","repository":{"id":20121210,"uuid":"23391093","full_name":"badoo/android-weak-handler","owner":"badoo","description":"Memory safer implementation of android.os.Handler","archived":false,"fork":false,"pushed_at":"2022-02-24T20:00:40.000Z","size":153,"stargazers_count":1546,"open_issues_count":5,"forks_count":288,"subscribers_count":79,"default_branch":"master","last_synced_at":"2025-03-31T23:34:15.143Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/badoo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-27T13:43:45.000Z","updated_at":"2024-12-07T14:36:56.000Z","dependencies_parsed_at":"2022-07-13T09:10:30.336Z","dependency_job_id":null,"html_url":"https://github.com/badoo/android-weak-handler","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badoo%2Fandroid-weak-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badoo%2Fandroid-weak-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badoo%2Fandroid-weak-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badoo%2Fandroid-weak-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/badoo","download_url":"https://codeload.github.com/badoo/android-weak-handler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247755489,"owners_count":20990618,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:16:46.246Z","updated_at":"2025-04-08T00:34:06.428Z","avatar_url":"https://github.com/badoo.png","language":"Java","funding_links":[],"categories":["Other","Libraries","Android 开发中的日常积累","Libs","库"],"sub_categories":["Utility","安卓开发值得关注的库","\u003cA NAME=\"Other1\"\u003e\u003c/A\u003eOther","[](https://github.com/JStumpp/awesome-android/blob/master/readme.md#utility)框架"],"readme":"Android Weak Handler\n====================\n\nMemory safer implementation of android.os.Handler\n\nProblem\n-------\n\nOriginal implementation of Handler always keeps hard reference to handler in queue of execution.\nAny object in Message or Runnable posted to `android.os.Handler` will be hard referenced for some time.\nIf you create anonymous Runnable and call to `postDelayed` with large timeout, that Runnable will be held\nin memory until timeout passes. Even if your Runnable seems small, it indirectly references owner class,\nwhich is usually something as big as Activity or Fragment.\n\nYou can read more [on our blog post](https://medium.com/bumble-tech/android-handler-memory-leaks-7291c5be6101).\n\nSolution\n--------\n\n`WeakHandler` is trickier than `android.os.Handler` , it will keep `WeakReferences` to runnables and messages,\nand GC could collect them once `WeakHandler` instance is not referenced any more.\n\n![Screenshot](WeakHandler.png)\n\nUsage\n-----\nAdd JitPack repository to your build.gradle:\n```groovy\nrepositories {\n    maven { url 'https://jitpack.io' }\n}\n\ndependencies {\n    implementation 'com.github.badoo:android-weak-handler:1.3'\n}\n```\n\nUse WeakHandler as you normally would use Handler\n\n```java\nimport com.badoo.mobile.util.WeakHandler;\n\npublic class ExampleActivity extends Activity {\n\n    private WeakHandler handler; // We still need at least one hard reference to WeakHandler\n\n    protected void onCreate(Bundle savedInstanceState) {\n        handler = new WeakHandler();\n        ...\n    }\n\n    private void onClick(View view) {\n        handler.postDelayed(new Runnable() {\n            view.setVisibility(View.INVISIBLE);\n        }, 5000);\n    }\n}\n```\n\nCredits\n-------\nWeak Handler is brought to you by [Badoo Trading Limited](https://corp.badoo.com) and it is released under the [MIT License](https://opensource.org/licenses/MIT).\n\n## Blog\nRead more on our [tech blog](http://techblog.badoo.com/) or explore our other [open source projects](https://github.com/badoo)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbadoo%2Fandroid-weak-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbadoo%2Fandroid-weak-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbadoo%2Fandroid-weak-handler/lists"}