Title: Optionally ignore @UIThread call when @EFragment is detached from Activity? · Issue #875 · androidannotations/androidannotations · GitHub
Open Graph Title: Optionally ignore @UIThread call when @EFragment is detached from Activity? · Issue #875 · androidannotations/androidannotations
X Title: Optionally ignore @UIThread call when @EFragment is detached from Activity? · Issue #875 · androidannotations/androidannotations
Description: When using @Background in conjunction with @UIThread annotations on an @EFragment, it's possible that many developers don't want to run the method annotated with the @UIThread annotation if the Fragment is no longer attached to the activ...
Open Graph Description: When using @Background in conjunction with @UIThread annotations on an @EFragment, it's possible that many developers don't want to run the method annotated with the @UIThread annotation if the Fra...
X Description: When using @Background in conjunction with @UIThread annotations on an @EFragment, it's possible that many developers don't want to run the method annotated with the @UIThread annotation if...
Opengraph URL: https://github.com/androidannotations/androidannotations/issues/875
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Optionally ignore @UIThread call when @EFragment is detached from Activity?","articleBody":"When using `@Background` in conjunction with `@UIThread` annotations on an `@EFragment`, it's possible that many developers don't want to run the method annotated with the `@UIThread` annotation if the Fragment is no longer attached to the activity. I think it would be great to add an annotation (something like `@IgnoredWhenDetached`) with the following rules: \n\n1) Must also have the `@UIThread` annotation\n2) Can only be used in conjunction with `@EFragment` classes\n3) Simply wraps the `@UIThread`method call in an if block like so\n\n``` java\nif(isAdded()) {\n //executable code\n}\n```\n\nor\n\n``` java\nif(getActivity() != null) {\n //executable code\n}\n```\n\nHere is a simple example and in my application I have dozens of things similar to this\n\n``` java\n@EFragment\npublic class LoaderFragment extends Fragment {\n\n @Background\n void longTask() {\n try {\n updateProgress(0);\n Thread.sleep(1000);\n updateProgress(50);\n Thread.sleep(1000);\n updateProgress(100);\n } catch (InterruptedException e) {\n killActivity() \n }\n }\n\n @IgnoredWhenDetached\n @UiThread\n void updateProgress(int progress) {\n getActivity().setProgress(progress);\n }\n\n @IgnoredWhenDetached\n @UiThread\n void killActivity(int progress) {\n getActivity().finish();\n }\n```\n\nThis essentially prevents the two `@UIThread` methods from getting a NPE if the Fragment were to have been destroyed at any point during the execution of the `@Background` method. While I realize this is something that can be easily guarded against by a simple null check, it makes the code much cleaner and is a very common use case in conjunction with Fragments. I would be more than happy to submit a pull request pending thoughts/comments about this. This could also be useful in conjunction with `@Background` on an `@EFragment` as well, but not a very common use case. \n\nVince\n","author":{"url":"https://github.com/vincentjames501","@type":"Person","name":"vincentjames501"},"datePublished":"2014-01-10T22:13:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/875/androidannotations/issues/875"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bc3dfa13-6587-f510-4bdd-a391797b2e16 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9AE6:A2CB5:16D4E22:2136214:6A609FCC |
| html-safe-nonce | 8b00a4df3e1e87b9ab2bbf35a754d455c194de94801acdcf97a19e915f29f752 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUU2OkEyQ0I1OjE2RDRFMjI6MjEzNjIxNDo2QTYwOUZDQyIsInZpc2l0b3JfaWQiOiI3OTI4NjUwMjYwMzE0ODkwMTg4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 157b6b8ac98ad601f583bbf5b27c57cdd934b2043caebf11ffb898a4eb0f906f |
| hovercard-subject-tag | issue:25426527 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/androidannotations/androidannotations/875/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b175f23dd951a0a7e103e77cdebce8be92eccf4da5f32be1c0d04d1ffccc446a/androidannotations/androidannotations/issues/875 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b175f23dd951a0a7e103e77cdebce8be92eccf4da5f32be1c0d04d1ffccc446a/androidannotations/androidannotations/issues/875 |
| og:image:alt | When using @Background in conjunction with @UIThread annotations on an @EFragment, it's possible that many developers don't want to run the method annotated with the @UIThread annotation if the Fra... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vincentjames501 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5142d23feb6a650085422edf60d68c93777dfa6c4b0c9151eb464d3e4969f618 |
| turbo-cache-control | no-preview |
| go-import | github.com/androidannotations/androidannotations git https://github.com/androidannotations/androidannotations.git |
| octolytics-dimension-user_id | 1315313 |
| octolytics-dimension-user_login | androidannotations |
| octolytics-dimension-repository_id | 3116751 |
| octolytics-dimension-repository_nwo | androidannotations/androidannotations |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3116751 |
| octolytics-dimension-repository_network_root_nwo | androidannotations/androidannotations |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c55ed501f6bbf0f43900d14552f6a9bd0bec6757 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width