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!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,616评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,020评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,078评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,040评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,154评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,265评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,298评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,072评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,491评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,795评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,970评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,654评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,272评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,985评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,815评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,852评论 2 351

推荐阅读更多精彩内容

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