Android MVC 设计模式

发布于 2024-11-15 06:56:58 字数 995 浏览 3 评论 0 原文

我正在寻找一些设计模式来控制 Android 应用程序中的一组 Activity。我目前正在开发一款应用程序,我认为该应用程序的 UI 将会经过多次修订,直到其“最终”版本(如果有的话)。我正在考虑在 Service 中使用控制器的 Observer 模式,但我找不到任何好的例子。我发现唯一常见的参考是使用 AIDL 进行进程间接口绑定,这是不适用的。

基本上我想要的是 Activity 实现定义的Interface,例如 showLoginScreen()loginError()等等,这样任何 UI 都应该能够实现(控制器不直接绑定到视图,只绑定到它的界面)。如果这是实现此目的的最干净的方法,那么获取活动 Activity 句柄的最佳接受方法是什么?我一直很困惑当您在不活动的 Activity 上调用方法时会发生什么。

我在想Application类中的Map作为单例? Mapput() / remove() 将与 onStart() 绑定onPause()。但这仍然不能保证 Activity 仍然存在...可以通过键上的 get() 获得引用,然后它可能是 Service有机会调用其接口之前>paused()。

任何建议或见解将不胜感激。

编辑:我看过其他帖子,例如 Android 上的 MVC 模式,但他们大多没有地址实施(无论如何,我只是断然不同意接受的答案)

I am looking for some design patterns to use controlling a set of Activitys in an Android application. I am currently developing an app which I feel will go under many many revisions of the UI until its "final" release (if there ever is one). I was thinking something along the lines of an Observer pattern using a controller in the Service but I can't find any good examples. The only thing I find common references to is using AIDL for inter-process interface binding, which will not be applicable.

Basically what I want is for the Activity to implement a defined Interface such as showLoginScreen(), loginError() etc. such that ANY UI should be able to implement (the controller is not tied directly to the view, only its interface). If this is the cleanest way to accomplish this, what is the best accepted way of getting handles to active Activitys? I have always been confused what happens when you invoke a method on an Activity that is not active.

I was thinking something along the lines of a Map in the Application class serving as a singleton? The put() / remove() of the Map would be tied to onStart() and onPause(). This still doesn't guarantee the Activity is still alive though...a reference could be gained with a get() on the key, and then it could be paused() before the Service has a chance to call its interface.

Any suggestions or insight would be appreciated.

edit: I have looked at other posts such as MVC pattern on Android however they mostly don't address implementation (and that accepted answer I just flat out disagree with anyways)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-11-22 06:56:58

要在您的活动和服务之间使用观察者/可观察模式,您需要使用 绑定服务

这样,您可以获得可以充当您的 Observable 的 IBinder 句柄,并且不必担心 AIDL。您可以确保服务已在 ServiceConnection 中的 onServiceConnected() 方法中绑定。请记住,只有当有 Activity 绑定到您的服务时,您的服务才会处于活动状态,否则它将被停止。

我建议阅读 android Bound Services 文档,因为它解释了使用非常好。

To use an observer/observable pattern between your activities and your service, you will need to use Bound services.

This way, you can get a handle to the IBinder which can act as your Observable and you do not have to worry about AIDL. You can ensure that the Service has been bound to in the onServiceConnected() method in your ServiceConnection. Keep in mind that your service will only be alive as long as there is an Activity bound to it, otherwise it will be stopped.

I would recommend reading the android Bound Services documentation as it explains the usage very well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文