开发者

Android separate responsibilities examples

开发者 https://www.devze.com 2023-01-20 09:17 出处:网络
Hi All One thing about Android development i would like to see some examples on are, separating responsibilities in Android, right now i have Activities and i feel its quite unpleasant, how i put all

Hi All One thing about Android development i would like to see some examples on are, separating responsibilities in Android, right now i have Activities and i feel its quite unpleasant, how i put all my event handling, UI construction/updating and communication with my data persistence into a single Activity. This makes my Activities extremly "fat", also because of the way eventlisteners are implemented, i have alot of classes inside other classes, (im used to the idea of one file => one class). I tried separating some of the communication to external webservice into Services, but now qui开发者_运维技巧te sure this is the best way to do it.

What patterns apply well when developing Android, and you want to separate responsibilities. Ive been working alot with Silverlight MVVM pattern, im looking for something similar that apply well to the android environment.


It is good practise to separete the functionality. Make a class or a whole package which works with database. The activity should just call the appropriate methods.

In generally, activity should just handle/create ui and delegate the business logic or other actions to other classes.


The MVP pattern you are talking about is in my opinion the best way to avoid to clutter your Activities.

Be sure that as much layout stuff as possible is put into xml, like statefull drawables for enabled, pressed disabled images or color state lists for enabled disabled colors.

Then create an activity that only instantiates the layout and passes down events to the correct controllers for packages in lower levels.

If you don't like the way especially onlick listeners are created (lots of anonymous classes) have a look at android:onclick this lets you specify which method should be called if the user presses the view right in your xml layout file just like it is done in some web tools.


I am the developer of Android-Binding, an Open Source Framework helps implementing MVVM in Android. It's a very new project but I do hope to get more buzz and experience so that it can be improved. Back to your question, I have written some simple introduction/tutorials on MVVM with android-binding:

  • Hello Android MVVM (with android binding)
  • Introduction to Android Binding (codeproject)
  • Model Validation in Android Binding (codeproject)
  • Wiki in project homepage

Potential adopters please also register on the project discussion group.

== Update ==

An article about applying MVVM pattern in Android is posted in Code Project:

  • http://www.codeproject.com/KB/android/AndroidMVVM.aspx
0

精彩评论

暂无评论...
验证码 换一张
取 消