Title: Sharing members between an activity and its fragment(s) · Issue #854 · androidannotations/androidannotations · GitHub
Open Graph Title: Sharing members between an activity and its fragment(s) · Issue #854 · androidannotations/androidannotations
X Title: Sharing members between an activity and its fragment(s) · Issue #854 · androidannotations/androidannotations
Description: Hello, as it is, the communication between Activity and Fragment classes is a little bit complicated. Bundle and intents work only for simple data. But at the moment there is no easy way to pass "real objects" onto activities (like DbHel...
Open Graph Description: Hello, as it is, the communication between Activity and Fragment classes is a little bit complicated. Bundle and intents work only for simple data. But at the moment there is no easy way to pass "r...
X Description: Hello, as it is, the communication between Activity and Fragment classes is a little bit complicated. Bundle and intents work only for simple data. But at the moment there is no easy way to pass &q...
Opengraph URL: https://github.com/androidannotations/androidannotations/issues/854
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sharing members between an activity and its fragment(s)","articleBody":"Hello,\n\nas it is, the communication between `Activity` and `Fragment` classes is a little bit complicated.\nBundle and intents work only for simple data. But at the moment there is no easy way to pass \"_real objects_\" onto activities (like DbHelper, or image downloaders...). \n\nWhat I propose a new annotation _@Shared_\n\n``` java\n@EActivity(R.layout.activity_main)\npublic class MainActivity extends Activity {\n\n @Shared\n String param1 = \"string from the activity\";\n\n @Shared\n Bundle param2;\n\n @Shared\n @Bean\n MyBean bean;\n\n @AfterInject\n void initInject() {\n // You can do custom member initialisation here\n param2 = new Bundle();\n param2.putString(\"are we\", \"done?\");\n }\n}\n\n```\n\nAnd \n\n``` java\n@EFragment(R.layout.fragment_main)\npublic class MainFragment extends Fragment {\n\n @Shared\n String param1 = \"string from the fragment\";\n\n @Shared\n Bundle param2;\n\n @Shared\n MyBean bean;\n\n @AfterViews\n void init() {\n // All the members are initialized and ready !\n }\n}\n\n```\n\nI did a little mockup to validate the idea and it works nicely.\n\nHere is the generated code \n\n``` java\npublic final class MainFragment_ extends MainFragment ... {\n\n @Override\n public void onViewChanged(HasViews hasViews) {\n ...\n initShared_();\n ...\n }\n\n private void initShared_() {\n if (getActivity() instanceof SharingActivity_) {\n SharingActivity_ act = (SharingActivity_) getActivity();\n param1 = (String) act.getSharedObject_(\"param1\");\n param2 = (Bundle) act.getSharedObject_(\"param2\");\n bean = (MyBean) act.getSharedObject_(\"bean\");\n }\n }\n\n // Reset the shared members to prevent memory leaks\n private void resetShared_() {\n param1 = null ;\n param2 = null ;\n bean = null ;\n }\n\n public void onDestroyView () {\n resetShared_() ; \n super.onDestroyView() ;\n }\n\n```\n\nThe parent activity implements `SharingActivity_` if it exposes shared members.\n\n``` java\npublic interface SharingActivity_ {\n\n Object getSharedObject_(String name);\n\n}\n```\n\n``` java\npublic final class MainActivity_ extends MainActivity implements\n ... SharingActivity_ {\n\n @Override\n public Object getSharedObject_(String name) {\n if (\"param1\".equals(name))\n return param1;\n if (\"param2\".equals(name))\n return param2;\n if (\"bean\".equals(name))\n return bean;\n Log.d( \"TAG\", \"The member '+name +\"' is not declared by the activity 'MainActivity' or is not annotated with @Share\" ) ;\n return null;\n }\n\n```\n\nThe `getSharedObject_` method returns something only for shared members.\n\nI think that along with [Otto](http://square.github.io/otto/) that could really ease the development with `Fragment`.\n\n**edit**: reset shared variables to prevent memory leaks\n","author":{"url":"https://github.com/PerfectCarl","@type":"Person","name":"PerfectCarl"},"datePublished":"2014-01-02T11:16:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":18},"url":"https://github.com/854/androidannotations/issues/854"}
| 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:8a70dae3-1d24-7e6e-a2d6-b7e624104a92 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AED4:877C4:4D54C5:6947BB:6A617D16 |
| html-safe-nonce | 7cd71e51bca524ff0153a9b183a5c3f48f36ec882dae0954b6a2b7d283a5b64d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUQ0Ojg3N0M0OjRENTRDNTo2OTQ3QkI6NkE2MTdEMTYiLCJ2aXNpdG9yX2lkIjoiNDQ5MjI3NDY0MjIwOTMwNzkyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cc72bace4f6774bb9cd99214e580ef0e18c103747c4c7d94e6cf510dd062fec6 |
| hovercard-subject-tag | issue:24959316 |
| 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/854/issue_layout |
| twitter:image | https://opengraph.githubassets.com/06acaaeffca4df4d56fde7c191c9947d3087e6a7a58d46c0bf1a809a20c2b742/androidannotations/androidannotations/issues/854 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/06acaaeffca4df4d56fde7c191c9947d3087e6a7a58d46c0bf1a809a20c2b742/androidannotations/androidannotations/issues/854 |
| og:image:alt | Hello, as it is, the communication between Activity and Fragment classes is a little bit complicated. Bundle and intents work only for simple data. But at the moment there is no easy way to pass "r... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PerfectCarl |
| hostname | github.com |
| expected-hostname | github.com |
| None | c28c2f6607f2aacbd934868d997038895c9780d08babfe918550d73773326f4c |
| 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 | cd05235d52ffb3318513d883a56c87a121015c8b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width