Android MVP:具有多个片段的一项活动 [英] Android MVP : One Activity with Multiple Fragments

查看:86
本文介绍了Android MVP:具有多个片段的一项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动",其中包含多个片段,这些片段定义了用户注册流程中的每个步骤.该流程很复杂,下一步是由前面步骤中的用户操作定义的.

I have an Activity that hosts multiple fragments that define each step in a registration flow for a user. The flow is complex and the next step is defined by user actions in the previous steps.

流程如下:

MainActivity | Fragment1 --> Fragment2 --> Fragment3 --> Fragment4
                                       \
                                        --> Fragment5 --> Fragment6

在获得MVP之后,我拥有了View&涉及的每个片段的演示者.我关心的是如何以MVP方式在Fragment和Activity之间进行通信.每个分片的结果必须可供活动使用,以决定接下来需要显示什么分片.

Following MVP, I have View & Presenter for each Fragment involved. The concern I have is how to communicate between the Fragment and the Activity in an MVP way. The result of each Fragment needs to be available to the Activity to decide what Fragment needs to be shown next.

我想到的一种方法是为每个由Activity实现的片段定义一个侦听器.我不知何故觉得这有点丑陋,因为最后,Activity需要实现其中的6个,并且看起来不具有可扩展性.

One of the approaches I have thought of is to have a listener defined for each fragment which is implemented by the Activity. I somehow find this a little ugly because in the end the Activity needs to implement 6 of these and doesn't look scalable.

有什么更好的方法?

推荐答案

您已经提到要使用MVP与片段进行通信.据我发现,有2种解决方案.

As you have mentioned that you want to communicate with fragments using MVP.According to me I found 2 solutions.

解决方案1: 使用接口进行回调,而不是创建一个接口&轰炸了许多需要对活动/片段执行的方法.尝试创建多个接口&根据您的功能添加方法&根据您的用途实施这些操作.

Solution 1: Callbacks using interfaces, rather creating one interface & bombarding lot of methods that need to be perform on activity/fragment. Try to create more than one interfaces & add methods according to your functionality & implement those according to your use.

解决方案2: 事件总线是使用MVP的另一种解决方案,您可以在其中观察总线&接送事件.处理事件总线时,请注意以下几点: 一种.如果您在片段活动"中使用发布或订阅者,则应在其生命周期中对其进行注册和注销.否则,您很可能会遇到内存泄漏或悬挂的引用,这些引用可能导致您的应用崩溃.

Solution 2: Event Buses is the other solution using MVP, where your view observes the bus & picks up event. Following points to take care when dealing with event buses: a. If you are using publishing or subscribers within an Activity of Fragment they should be registered and unregistered with their lifecycle. Otherwise, it's likely you will encounter memory leaks or dangling references that can cause your app to crash.

b.警惕在片段之间发布事件.片段未运行时,无法发布或接收事件.如果您有一个Fragment将消息发布到当前未执行的另一个Fragment,然后将其中一个交换为另一个,则可能无法正确处理该事件. EventBus库提供了一种重播此事件的方法,但是Otto框架没有.

b. Be wary about publishing events between Fragments. Events cannot be published or received when a Fragment is not running. If you have a Fragment publishing a message to another Fragment that isn't currently executing and then swap one for the other, it's likely the event will not be processed correctly. The EventBus library has a way to replay this event, but the Otto framework does not.

对于事件总线,您可以引用两个库: -GreenBot: https://github.com/greenrobot/EventBus -奥托(Otto): http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto /article.html

For Event Buses you can refer two libraries: - GreenBot : https://github.com/greenrobot/EventBus - Otto : http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto/article.html

结论 如果您要处理活动/片段上的小操作,那么处理接口是更好的选择.事件总线可提高您的应用程序性能,并增强对事件总线的依赖范围.

Conclusion If you are dealing with small operations on Activity/Fragments then dealing with interfaces is better option. Event Buses increases your app performance, also dependencies for larger scopes you can go for event buses.

这篇关于Android MVP:具有多个片段的一项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆