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!
网友评论