Passing Data between Arbitrary amounts of Fragments via one Activity

随声附和 提交于 2020-01-26 03:36:19

问题


I have an application design question regarding passing data between fragments. I have an application using the navigation drawer layout, with various "sub applications" that perform standalone functions (for example, a calculators app, with unrelated "sub apps" for income tax calculations, metric/imperial conversions, etc).

For a given calculator, I'd have a data entry/selection fragment and a data presentation fragment that displays the calculation in some meaningful type of way. Thus, my only activity in the application is the MainActivity holding the navigation drawer and content pane. The question is: what is the best way to design the application such that the various calculators pass data to their data representation fragments, say on the click of a button? It is my understanding that fragment are not designed to pass data between each other, but rather rely on an activity to facilitate that via some type of callback listener. However, with only one activity and an arbitrary amount of calculators, how does this work? Am I supposed to create a separate activity for each calculator? If so, how does this affect the navigation drawer?

Note that the example is completely made up. Thanks for your help!

来源:https://stackoverflow.com/questions/22749483/passing-data-between-arbitrary-amounts-of-fragments-via-one-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!