Android development specifications (beginning)

Source: Internet
Author: User
First, let's talk about coding specification 1 and Java of Android development specifications. Code No Chinese characters appear in. Chinese characters can appear in the comments at most, and Chinese characters are written in strings. XML; 2. Local variable naming and static member variable naming can only contain letters, the first letter of a word is capitalized, And the other letters are lowercase 3. Constant naming can only contain letters and _, and all letters are capitalized, 4 words are separated by _. The ID naming mode in the layout file is: View abbreviation _ module name_view logical name common control abbreviations are as follows: layoutview: lv_relativeview: rv_textview: TV _imageview: iv_imagebutton: im_button: btn_5, strings. id naming mode in XML: Activity name_function module name_logic name/activity name_logic name/common _ logic name strings. in XML, use the activity name annotation to distinguish the file content from 6, Styles. XML: extract the styles that are repeatedly reproduced in layout to produce general styles. Handler 11. Exercise caution when using static variables to share interfaces. 12. Log (system name module Name Interface Name, detailed description). 13. Do not reuse handler of the parent class, handler corresponding to a class should not be used by its subclass; otherwise, it will cause message. what conflict 14, strings. in XML, % 1 $ S is used to implement the character string wildcard 15. If multiple activities contain common UI processing, you can extract a commonactivity and call the common part for processing, other activities only need to inherit it, and the data must be validated. For example, if the conversion fails, there must be a default value. Whether the server responds to the data is valid or not. OK. Next, let's talk about it, how to improve the efficiency of Android To optimize the performance of Android: 1. Http uses gzip compression to set the connection timeout and response timeout. HTTP requests can be divided into cache and non-cache based on service requirements, in a network-less environment, some data is still browsed through the cached httpresponse for offline reading. 2. listview performance optimization 1). Reuse convertview in getitemview to determine whether convertview is empty. If it is not empty, it can be reused. If you need to add listerner to the view in couvertview, the Code must be out of IF (convertview = NULL. 2 ). if the image item contains a webimage, it is best to load it asynchronously. 3 ). the image is not displayed during quick slide. when the list is quickly swiped (scroll_state_fling), the image in the item or the view that consumes resources can be retrieved and not displayed; in the other two States (scroll_state_idle and scroll_state_touch_scroll), the views are displayed 4 ). baseadapter prevents memory overflow. If the entity class of baseadapter has attributes that consume a lot of memory, it can be saved to files. To improve performance, it can be cached and the cache size can be limited. 3. the thread pool is used, which can be divided into the Core Thread Pool and general thread pool. The time-consuming tasks such as downloading images are placed in the General thread pool to prevent time-consuming tasks from blocking the thread pool, as a result, all asynchronous tasks must wait 4. asynchronous tasks are divided into core tasks and common tasks. errors are reported only when system errors occur in core tasks. The UI operation of asynchronous tasks needs to determine whether the original activity is active. avoid using static member variables to reference instances that consume too much resources, such as context6. use weakreference instead of strong references. Weak references allow you to retain references to objects and allow GC to release objects when necessary, reclaim memory. For those objects that create cheap but consume a large amount of memory, that is, you want to keep this object, and Program If you want to recycle GC when necessary, you can use weak references. 7. super fat bitmap timely destruction (bitmap is recycled when ondestroy of activity is used by the UI component. runtimeexception: canvas: trying to use a recycled bitmap android will be thrown after being recycled immediately by the UI component. graphics. bitmap) sets a certain sampling rate (sampling is not required for images provided by developers. For images uploaded by users or uncontrolled by third parties, sampling can be performed to reduce the memory occupied by images ), when returning images from the server, we recommend that you use soft references to the resid resources of the drawable image. Bitmap corresponds to any type of images of other resources. If it cannot be obtained (for example, the file does not exist, or when an outofmemory exception is run during file reading, there should be a corresponding default image (the default image is placed in the APK and obtained through resid); 8. ensure that the memory occupied by cursor is promptly released, rather than waiting for GC to process. Android obviously prefers programmers to manually close the cursor. 9. The thread is also an important source of Memory leakage. The main cause of thread Memory leakage is the uncontrollable thread lifecycle 10. if the imageview image is from the network, perform asynchronous loading 11. during custom view development, do not write the interaction part as a thread to refresh the interface display constantly. Instead, update the interface automatically triggered Based on the touchlistener event 12. the image used by the drawableui component is included in the APK package. Therefore, setimageresource or setbackgroundresource is used instead of resourceid. Note: Get (getresources (), R. drawable. btn_achievement_normal) This method is converted to drawable through resid. You need to consider the recycling problem. If drawable is a private object of the object, the memory will not be released before the object is destroyed. 13. reuse and Recycle activity objects temporary activity timely finish main interface set to singletask general interface set to singletop14. location information to get the user's geographic location information, when you need to get data turn on GPS, then close it in time. in onresume, set the power management on this interface, and cancel the setting in onpause.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.