Android MVP:哪一层应存储上下文变量 [英] Android MVP: which layer should store context variable

查看:119
本文介绍了Android MVP:哪一层应存储上下文变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击视图上的按钮时,我发现自己需要在哪里播放声音文件.

I find myself where i need to play a sound file when user clicks a button on a view.

MediaPlayer需要创建上下文.

MediaPlayer requires a context to be created.

放置MediaPlayer初始化代码的最佳方法是什么?

What is the best way to put MediaPlayer initialization code?

我应该将上下文传递到presenter方法中并在其中播放吗?

Should I pass a context into a presenter method and play it there?

还是可以只在视图上播放就可以了.

Or is it ok to just play on the view.

推荐答案

Context是MVP中Android View层的一部分,因此Presenter不得对此有任何想法,并且不应将其传递给presenter.

Context is a part of Android View Layer in MVP, so Presenter must not have any idea about it and you should not pass it to presenter.

您必须在View界面中添加一个方法,并在android视图组件(即ActivityFragment)中实现该方法,并使用它们在View层中播放声音.

You have to add a methods to your View interface and implement it inside your android view components (i.e. Activity or Fragment) and use them to do an action in View layer as playing a sound.

Presenter必须询问UI事件,而View必须处理它!

Presenter must ask for the UI event and View must handle it!

这是使用 Dagger RxJava Retrofit MVP 示例,这可能有助于您了解更多信息关于Android中的MVP:

Here is a MVP sample using Dagger, RxJava and Retrofit, which might help you to learn more about MVP in Android:

https://github.com/mmirhoseini/marvel

这篇关于Android MVP:哪一层应存储上下文变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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