Android 上开发的架构模式
我正在开始 Android 应用程序开发,就在我的第一个应用程序比 Hello World 更复杂之后,我发现您无法在 Activity 中保留任何状态,因为 Android 在某些情况下会重新创建它们......哎呀,失败。 我猜想严格的模型与视图分离是一种常见的做法,即使对于简单的应用程序也是如此。有什么我应该遵循的既定模式吗?模型-视图-控制器的某种变体?
I'm starting Android app development and just after my very first app more complex than Hello World I found out that you cannot hold any state in Activity because Android will recreate them in some cases... ouch, failure.
I'm guessing that strict model separation from view is a common practice, even for simple apps. Are there any settled patters I should follow? Some variant of Model-View-Controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对应该阅读应用程序基础开发指南
根据我的经验,我的设计模式Android 是通过使用
Intents
、Activities
、Services
、Broadcast receiveers
等来驱动的。我不会说“严格的模型与视图分离是一种常见的做法”。您可以将数据保留在活动中,但您将被迫保留它。幸运的是,通过
onSaveInstanceState
和onRestoreInstanceState
等方法可以使这一过程变得微不足道,其中持久数据成员可以保存到Bundle
中,然后从同一个中检索>捆绑
。You should definitely read the Application Fundamentals Dev Guide
In my experience, my design patterns in Android have been driven by the use of
Intents
,Activities
,Services
,Broadcast receivers
and so forth.I wouldn't say that "strict model separation from view is a common practice". You can keep data inside an Activity, but you will be forced to preserve it. Luckily this is made trivial by methods like
onSaveInstanceState
andonRestoreInstanceState
where persistent data members can be saved to aBundle
and then retrieved from the sameBundle
.