Android coding specifications and performance optimization, and android Performance Optimization

Source: Internet
Author: User

Android coding specifications and performance optimization, and android Performance Optimization

Recently, we have seen that there are too many problems with the new android code, and there are some basic problems. So we have re-trained the code specifications, hoping that we will be able to get better later.

I. Android coding specifications

1. the java code does not contain Chinese characters. A maximum of Chinese characters can appear in comments.

2. Local variable naming, static member variable naming can only contain letters, the first outer Letter of the first letter of the word, all are in upper case, other letters are in lower case

3. Constant names can only contain letters and _. All letters are in uppercase and separated _.

4. The id naming mode in layout is: view abbreviation _ module name_view logical name view abbreviation details: LayoutView: lvRelativeView: rvTextView: tvImageView: ivImageButton: imButton: btn

5. the naming mode of view variables in the activity is logical name + view abbreviation. Suggestion: If the layout file is complex, we recommend that you divide layout into multiple modules. Each module defines a moduleViewHolder, its member variables include the view

6. 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 differentiate the file content. 7. in drawable, the image naming mode is activity name_logical name/common _ logical name.

7. styles. xml: extract the styles that are repeatedly reproduced in layout into general style components and put them in styles. xml;

8. Use layer-list and selector

9. Split images into multiple reusable images as much as possible

10. What the server can achieve is not to be placed on the client.

11. Exercise caution when referencing a third-party library to avoid using a large third-party library, resulting in a large client package.

12. Handle global application exceptions and errors and send the errors to the server by email.

13. process image. 9

14. Exercise caution when using static variables to share Interfaces

15. Log (system name module Name Interface Name, detailed description)

16. unit test (logical test and Interface Test)

17. Do not reuse the handler of the parent class. The handler of the corresponding class should not be used by its subclass. Otherwise, the message. what conflict will occur.

18. process all logic in a View. OnClickListener in the activity

19. Use % 1 $ s in strings. xml for character string wildcard

20. 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.

21. When you use button + activitgroup to implement the tab effect, use Button. setSelected (true) to make sure that the button is in the selected State and that the current activity of activitygroup corresponds to the button.

22. If a common component is developed, prefix the file name in the drawable/layout/menu/values directory to avoid conflicts.

23. data must be validated. For example, if the numeric type is converted to the numeric type, the default value must be set if the conversion fails; whether the server responds to the data effectively or not;

Ii. Android Performance Optimization

1. http uses gzip compression to set connection timeout and response timeout. http requests are 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.

3. the thread pool is divided into the Core Thread Pool and general thread pool. time-consuming tasks such as image download are placed in the General thread pool to avoid waiting for all asynchronous tasks after time-consuming tasks block the thread pool.

4. asynchronous tasks are divided into core tasks and common tasks. Only system-level errors in core tasks report errors. The ui operation of asynchronous tasks needs to determine whether the original activity is active.

5. Avoid using static member variables to reference instances that consume too much resources, such as Context.

6. Use WeakReference to replace strong references. Weak references allow you to keep references to objects, and allow GC to release objects and reclaim memory when necessary. For those objects that create cheap but consume a large amount of memory, that is, you want to keep the object and use it when the application needs it. If you want GC to recycle it when necessary, you can consider using weak references.

7. Super Big Fat Bitmap timely destruction (bitmap is recycled when onDestroy of Activity) sets a certain sampling rate skillfully using soft reference drawable corresponding to resid resources, bitmap corresponding to other resources.

8. Ensure that the memory occupied by Cursor is promptly released, rather than waiting for GC to process. In addition, Android obviously prefers programmers to manually close Cursor.

9. threads are 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, load it asynchronously.

11. During custom views in application development, do not write the interaction part as a thread to constantly refresh the interface display, but actively trigger interface updates based on the TouchListener event.

3. AndroidUI Optimization

1. layout componentization, use merge and include reuse whenever possible

2. Use styles to reuse style definitions

3. the pop-up control of the soft keyboard should not overwrite the input box

4. Placement of numbers, letters, and Chinese characters: All digits and letters are divided into different types. In most cases, because our input is half-width, the placeholder values of letters and numbers cannot be determined. However, once fully divided, the placeholder values of numbers and letters are the same as those of a Chinese character, in this way, the layout problem caused by placeholder can be avoided.

5. English Document Layout: When textview is automatically changed, the word integrity should be maintained. The solution is to calculate the string length, and then manually set the number of letters displayed in each row with 'N' added'

6. Use RelativeLayout for complex la s

7. adaptive screen; Use dp to replace pix

8. Use android: layout_weight or TableLayout to create an equals Layout

9. Use animation-list to create an animation

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.