{"id":17362036,"url":"https://github.com/ipcjs/view-removecallbacks-bug-demo","last_synced_at":"2025-03-27T16:46:11.553Z","repository":{"id":85339130,"uuid":"167137614","full_name":"ipcjs/View-removeCallbacks-Bug-Demo","owner":"ipcjs","description":null,"archived":false,"fork":false,"pushed_at":"2019-01-23T08:03:36.000Z","size":128,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T15:03:05.041Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ipcjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-23T07:26:52.000Z","updated_at":"2019-02-08T11:59:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"8e8e286e-4b14-4a3a-a5d0-0c4dd8acdc0f","html_url":"https://github.com/ipcjs/View-removeCallbacks-Bug-Demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipcjs%2FView-removeCallbacks-Bug-Demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipcjs%2FView-removeCallbacks-Bug-Demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipcjs%2FView-removeCallbacks-Bug-Demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipcjs%2FView-removeCallbacks-Bug-Demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ipcjs","download_url":"https://codeload.github.com/ipcjs/View-removeCallbacks-Bug-Demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245887859,"owners_count":20688893,"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-10-15T19:36:46.664Z","updated_at":"2025-03-27T16:46:11.521Z","avatar_url":"https://github.com/ipcjs.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# View-removeCallbacks-Bug-Demo\n\n复现`View.removeCallbacks`移除回调失败的bug\n\n## 复现场景\n\nview未关联到window时, 使用`view.postDelay(runnable)`发送runnable, 关联到window之后, 使用`view.removeCallbacks(runnable)`移除runanble, 则有可能复现该bug\n\n## 产生原因(推测)\n\n(以下分析以Api21的源码为例)\n\n1. 当View没有关联window时, `View.postDelay(runnable)`发送的runnable会放到[ViewRootImpl.getRunQueue()](https://github.com/aosp-mirror/platform_frameworks_base/blob/lollipop-release/core/java/android/view/ViewRootImpl.java#L6518)中\n2. window每次执行`performTraversals()`时, 都会将`runQueue`中的runnable取出来放到自身的`mHandler`中执行([详见源码](https://github.com/aosp-mirror/platform_frameworks_base/blob/lollipop-release/core/java/android/view/ViewRootImpl.java#L1323))\n3. `runQueue`是线程本地的, window是主线程中的, 但一个主线程可能有多个window(相同进程中的多个Activity)\n4. 已示例为例, `MainActivity`打开`NextActivity`时, 若在NextActivity关联到window之前MainActivity执行了一次`performTraversals()`, 则NextActivity中post的runnable会跑到MainActivity的mHandler中执行, 导致在NextActivity中remove失败\n\n## 规避方法\n\n如示例所示, 使用[CancelableRunnable](app/src/main/java/com/github/ipcjs/view_removecallbacks_bug_demo/CancelableRunnable.kt)替代Runnable\n\n## 出现bug的版本\n\nApi24+已经修复了, 相关提交应该是这个: \u003chttps://github.com/aosp-mirror/platform_frameworks_base/commit/bea0c7daa6611d8b96e1271f8854f500a87342fc\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipcjs%2Fview-removecallbacks-bug-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fipcjs%2Fview-removecallbacks-bug-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipcjs%2Fview-removecallbacks-bug-demo/lists"}