Summary of some development habits of Android.

Source: Internet
Author: User

Walk

Links: https://www.zhihu.com/question/27227425/answer/35973793

Source: Know

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.


One, the Android code specification
The 1.java code does not appear in Chinese, the most comments can appear in Chinese;
2. Local variable name, static member variable name: can only contain letters, the first letter of a word is uppercase, the other letters are lowercase;
3. Constant naming: can only contain letters and _, all uppercase letters, words separated by _;
ID naming in 4.layout: Named mode: The logical name of the view abbreviation _ Module name _view
The details of view abbreviations are as follows
Linearlayout:ll
Relativelayout:rl
Textview:tv
Imageview:iv
Imagebutton:ib
Button:btn
Name of view variable in 5.activity
Named mode: view abbreviation + logical name
Recommendation: If the layout file is complex, it is recommended that layout be divided into modules, each of which defines a moduleviewholder whose member variables contain the view
Name of ID in 6.strings.xml:


Naming pattern: Activity Name _ Function Module Name _ Logical name Activity name _ logical name Common_ logical name
Strings.xml, use activity name comments to separate the contents of the file
Picture naming in 7.drawable
Naming pattern: Activity Name _ logical name/common_ logical name
7.styles.xml: The continuous style of layout is extracted from the common style generic components, put into the styles.xml;
8. Using Layer-list and Selector
9. Image as many reusable pictures as possible
10. The server can be implemented, do not put on the client
11. Refer to third-party libraries be cautious, avoid the application of large-capacity third-party libraries, resulting in a very large client package
12. Handle the application of global exceptions and errors and send errors to the server by mail
13.9 Processing of pictures.
14. Use static variable mode to realize the sharing between interfaces be careful
15.Log (System name Module name Interface name, detailed description)
16. Unit Test (logic test, Interface test)
17. Do not reuse the handler of the parent class, the handler of a class should not be used for its subclasses, or it will cause message.what conflicts
Handle all logic in a view.onclicklistener in 18.activity
Using%1 $ s in 19.strings.xml to implement string wildcard
20. If more than one activity contains common UI processing, you can refine a commonactivity, call the generic part to handle it, and other activity just inherit it
21. When using Button+activitgroup to implement the tab effect, use button.setselected (true) to ensure that the button is selected and that the current activity of activitygroup corresponds
22. If you are developing a generic component, to avoid conflicts, add a prefix to the file name in the Drawable/layout/menu/values directory
23. Data must be tested, such as
The character type turns the numeral type, if the conversion fails must have the default value;
Whether the service-side response data is effectively judged;

Second, Android performance optimization
1.http with gzip compression, set the connection time-out and response timeout
The HTTP request is divided into whether it can be cached and not cacheable according to the business demand, then, in the network-free environment, the cached HttpResponse is still used to browse some data and realize offline reading.
2.listview Performance Optimization
1). Multiplexing Convertview
In Getitemview, determine if the Convertview is empty, and if it is not empty, it can be reused. If the view in Couvertview needs to add Listerner, the code must be outside the if (Convertview==null) {}.
2). Loading pictures asynchronously
If the item contains Webimage, it is best to load asynchronously
3). Do not show pictures when sliding quickly
When a quick slide list (scroll_state_fling), the picture in item or the view that needs to consume resources, can not be displayed, but in the other two states (Scroll_state_idle and Scroll_state_touch_ SCROLL), the view is displayed
3. Use a thread pool, divided into core thread pool and normal thread pool, download pictures and other time-consuming tasks placed in the normal thread pool, to avoid time-consuming tasks blocking the thread pool, resulting in all asynchronous tasks must wait
4. Asynchronous tasks, divided into core tasks and common tasks, only the core tasks in the system-level errors will be error, the UI operation of the asynchronous task needs to determine whether the original activity is active
5. Try to avoid the use of static member variables to reference resources that consume too many instances, such as context
6. Using WeakReference instead of a strong reference, a weak reference allows you to keep a reference to the object while allowing the GC to release the object and reclaim memory if necessary. Consider using weak references for those objects that create inexpensive but expensive memory, want to keep the object, and use it when the application needs it, while you want the GC to be recycled as necessary.
7. Super Big Fat Bitmap
Timely destruction (when the activity is OnDestroy, the bitmap will be recycled)
Set a certain sample rate
Clever use of soft references
drawable corresponding to Resid resources, bitmap corresponding to other resources 8. Ensure that the memory that the cursor occupies is released in a timely manner, rather than waiting for the GC to handle it. And Android is obviously prone to programmers manually close the cursor off
9. Threading is also an important source of memory leaks. The main reason for a thread's memory leak is that the thread life cycle is not controllable
10. If the picture of ImageView is from the network, it is loaded asynchronously
11. In the application development of Custom view, the interactive part, do not write thread constantly refresh the interface display, but based on the Touchlistener event active trigger interface update


Third, Android UI optimization
1.layout Modular, use merge and include reuse as much as possible
2. Using styles, reusing style definitions
3. Soft keyboard pop-up control, do not let it overwrite the input box
4. Number, letter, and character mix placeholder problem: Full-width of numbers and letters. Since most of the time our input is half-width, the placeholder for letters and numbers cannot be determined, but once full-width, the digits and the letters occupy the same position as a Chinese character, which avoids the typographical problems caused by the occupying position.
5. English document typesetting: TextView to maintain the integrity of the word when it wraps, the solution is to calculate the length of the string, and then manually set how many letters each line displays and add ' n '
6. Complex layout using Relativelayout
7. Adaptive screen, using DP instead of PIX
8. Use Android:layout_weight or tablelayout to make an equal layout
9. Animating effects with Animation-list


Iv. Some other Android development recommendations

1, keep up with the pace of the Times, the eclipse into Android Studio, the SVN to git, which of course to fit the needs of project development attributes;

2, diligent to do a summary, recommend the use of impression notes, some understand the experience summed up, the article still do not understand the inside, there is time to slowly digest;

3, regular code review, constantly iterative, you will always find some unreasonable code, or need to optimize the place.

4, pay attention to some well-known technology Big V or website, inside many things worth your digestion, recommended: Android Open Source project collection,/http/tech.meituan.com/, Stormzhang, but always return to say, Go to the Android website or refer to a copy of the Java API documentation although boring, but familiar with, you will have greater progress.

5. If you want to learn more about the articles that can be read for a long time: Android application development and deep analysis of design ideas behind

6, if your company does not have a strong Test team, before releasing the application is best to put the application on the test platform to measure, such as cloud measurement;

7, take the application package name should not be too easy to repeat, if the same mobile phone already has the package name, then the signature will cause the installation is not, it is also strange in the national security market too much, can not be like Google Play as the package name audit.

Summary of some development habits of Android.

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.