Android MVP:哪一层应存储上下文变量 [英] Android MVP: which layer should store context variable
问题描述
当用户单击视图上的按钮时,我发现自己需要在哪里播放声音文件.
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视图组件(即Activity
或Fragment
)中实现该方法,并使用它们在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屋!