使用MVP在android中传递捆绑意图 [英] Pass bundle intent in android using MVP

查看:85
本文介绍了使用MVP在android中传递捆绑意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过捆绑意图使用Parceler将模型数据传递到另一个活动中.我的问题是如何使用Android中的MVP架构将数据从Presenter传递到View层以在另一个活动中显示?

I want to pass the Model data into another activity using Parceler through Bundle intent. My problem is how could I pass the data from Presenter into the View layer to display in another activity using MVP architecture in android?

推荐答案

这当然是可能的.假设您的Activity实现了View界面,您将在界面中找到一个方法,例如:

This is certainly possible. Presuming that your Activity implements your View interface you'd have a method in the interface like:

void startNextActivity(MyData data);

然后在活动中:

@Override
void startNextActivity(MyData data) {

    // create bundle
    // send intent
}

在Presenter中:

And in Presenter:

view().startNextActivity(myData);

但是我不建议您这样做

我认为在进行MVP时应谨慎使用许多 classic Android模式.其中包括onActivityResult&使用Bundle在活动/片段之间传递数据.

I'm of the opinion that quite a few classic Android patterns should be used sparingly when doing MVP. This includes things such as onActivityResult & passing data around between Activities/Fragments using Bundle.

为使事物尽可能分离和干净,活动应避免与其他活动交谈,演示者不应与其他演示者交谈,等等.如果您需要访问另一个活动中一个活动的数据,则将其发送到模型中.坚持不懈.然后,下一个活动将由其演示者发送给该数据,该演示者将从模型中获取该数据.

To keep things as decoupled and clean as possible Activities should avoid talking to other Activities, Presenters shouldn't talk to other Presenters, etc. If you need to access data from one Activity in another Activity then send it to the model to be persisted. The next Activity will then be sent this data by its Presenter which will have got it from the model.

下图提供了更好的概述:

The following diagram gives a better overview:

它们没有在启动下一个活动时作为包的一部分传递详细信息,而是将其保留在模型中以供下一个活动加载.

Rather than passing the details as part of the Bundle when starting the next Activity they are persisted in the model for the next Activity to load.

这篇关于使用MVP在android中传递捆绑意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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