How do you create your Android app's architecture?

Android apps are easy to develop but very prone to spaghetti code, made up of bunch of copy pasted code snippets. However, some rules if followed allow you to develop elegant codebase for your android apps based on my experience as an Android developer and the mistakes I found while doing code reviews. Here are a few ways to design good apps that will stand the test of time and will prevent maintenance headaches in future:

MVC: Make sure you have code divided into models, views and controllers. This is one of the basic rules you can/should follow for any Object oriented codebase (Android app code being mostly Java, it applies very well here). For Android, roughly consider Activity class and XML as the view, a separate class to talk to backend and give and take from the UI as the controller and a model class that represent objects like student, teacher, human, animal.

Mirroring Database tables to Model classes: If you have aUsertable make sure you have aUserclass which has the exact same instance variables as the database columns. This allows you to represent each row in a database as a singular object of class user. This is very useful for data processing and storing results of select queries.

Inner classes andonClick()methods: Many times developers end up writing hundreds of lines longonClick()methods which are written inside an anonymous inner classes. Please avoid that for better readability. Refactor the code insideonClick()methods into smaller methods and call them inside theonClick()method.

Avoid too manyOnClickListeners: If your Activity class has a lot of buttons and correspondingonClickListeners, refactor them into customized classes outside of the activity class for better readability. By doing this though, we may lose the access control provided by the very nature of inner class. One way to fix that partially would be to declare the newOnClickListenerclass as protected and keep it in the same package where the activity class resides. This prevents any other class outside of the activity's package to access theOnClickListeners

Offline mode: Allow users to access to all the features of the app as far as possible without internet connectivity by temporarily storing their information inside the app and then syncing with the cloud backend once connected to internet. This helps in apps becoming successful in regions where the network connectivity is bad.

Do not store PII locally: Do not store personally identifiable information inside an android app locally.

Use SSL: To connect with your cloud vendor using REST API or by other means, always use a secure HTTP connection.

Do not hardcode UI element names: Always use strings.xml to store all the UI element names. Makes it easy to rename in future.

Multiple resources for Screen sizes: Try to use multiple resources for different screen sizes. Also try to create different layouts for portrait and landscape orientation. Please refer Android guidelines for supporting multiple screens:Supporting Multiple Screens

Use UUID for uniquely identifying users: Do not use IMEI, IMSI for uniquely identifying your users. This blogpost will help you understand why and how to use UUID:Identifying App Installations

Hope this helps new and existing fellow developers. Good luck!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 7,593评论 0 3
  • By clicking to agree to this Schedule 2, which is hereby ...
    qaz0622阅读 5,401评论 0 2
  • 2018-10-02 姓名:符振华(378期 反省三组) 公司:深圳蔚蓝时代商业管理有限公司-上海第一分公司 【日...
    Hank_dec5阅读 728评论 0 0
  • 要是我说错了话 用眼神杀死我就好 语气太过强烈 小心脏受不了
    Beijing阅读 916评论 0 0
  • 已所不欲,勿施于人。就这一句话,人人都做到,就能令社会和谐。 人生三问(向内求),我是谁,从哪...
    趣生活轻创业阅读 4,003评论 5 12