在 Android 中处理 Activity 的多个实例的最佳方法
我想在我的应用程序中制作动态表单,表单的数量将在运行时决定(1 到 n)。为了实现这一点,我创建了一个扩展 Activity 的 From Class。我的应用程序可以有此 Form 类的多个实例,并从一个 From 导航到另一个。
在同一类(来自类)的多个实例之间实现和通信的最佳方式是什么?
如何在两个表单之间进行通信(使用 Intent..?)。
或
另一种选择是我为所有表单采用一个活动[作为应用程序],并在同一活动上呈现每个表单?
I want to make dynamic Form(s) in my application, the number of forms will be decided at the run time(1 to n). To implement this I create a From Class which extends Activity. My application can have multiple instances of this Form class and navigate from one From to another.
What is the best way to implement and communicate between multiple instances of same Class(From Class).
How to communicate between two form (using Intent..?).
OR
The other option is I take one Activity[As application] for all the forms and render every form on same activity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从最终用户的角度来看,为不同的表单提供多个独立的活动可能不是最明显的方法。您可能需要考虑使用 TabLayout 将它们分组,甚至使用 FrameLayout 和菜单切换器。
无论哪种方式都会简化信息交换。如果使用FrameLayout,那么一切都是同一个Activity,所以问题就变得微不足道了。如果您使用 TabLayout(也许是最好的选择),那么您可以在 TabActivity 类上存储公共 ArrayList,从而再次使交换变得非常简单。
如果您想遵循独立的活动路径,则需要考虑的其他选项是意图上的附加项(例如,一个附加项可能是表单编号,另一个是要发送的数据)。然后,活动可以在 if/else 块内处理这些额外内容,以将额外内容中的表单编号与私有字段进行比较。
最后,您可以将数据存储在应用程序实例中(始终只有一个实例)并从每个活动访问它。
Having multiple independent Activities for the different forms may not be the most obvious way from the end user point of view. You may want to consider a TabLayout to group them, or even a FrameLayout and a menu switcher.
Either way will simplify the information exchange. If using the FrameLayout, then everything is the same Activity, so the problem becomes trivial. If you use the TabLayout (perhaps the best choice), then you can store a public ArrayList on the TabActivity class, making again the exchange quite simple.
Other options to consider, if you want to follow the independent Activities path, are extras on the Intents (eg., one extra could be the form number, and another the data to be sent). Then, the activities may handle those extras inside an if/else block to compare the form number in the extra with a private field.
Lastly, you can store the data in an Application instance (there's always one single instance) and access it from each Activity.
您的另一个选择是正确的选择...使用单个活动并更新其内容以匹配您当前关注的内容。这将大大减少应用程序以及系统资源的开销。
Your other option is the right one to take... use a single activity and update its contents to match those of whatever you're currently focusing on. This will have much less overhead within your application as well as on system resources.
我认为第二种解决方案听起来更像是“正确的方法”
我建议不要实现不同的活动,而是创建多个视图(例如“小部件”)更好的解决方案,这些视图代表您需要的当前表单。
无论如何,如果你想在 Activity 之间进行通信,我认为最好的方法是通过 Handler-Objects 来实现。这些Handler可以处理你可以在其中定义的Message,并在里面放入大量的Data。
I think that the second solution sounds much more like "the right way to do it"
I suggest instead of Implementing different Activitys, its a better solution to create multible Views like "Widgets" which are a representation of the current form you need.
Anyway, if you want to communicate between Activitys, I think the best Way is, to do this through Handler-Objects. These Handlers can handle Messages in which you can define, and put a lot of Data inside.