Android programming specifications and common techniques
Source: Internet
Author: User
<span id="Label3"></p>one, the Android Code specification<br>Chinese is not present in 1.java code, the maximum number of comments can appear in Chinese<br>2. Local variable naming, static member variable naming<br>Can only contain letters, the first letter of a word, all uppercase, the other letters are lowercase<br> <br> <br>3. Constant naming<br>Can only contain letters and _, all uppercase letters, between words separated by _<br> <br> <br>Name of the ID in 4.layout<br>Named Mode: the logical name of the view abbreviation _ Module name _view<br> <br> <br>The details of view abbreviations are as follows<br> <br> <br>Layoutview:lv<br> <br> <br>Relativeview:rv<br> <br> <br>Textview:tv<br> <br> <br>Imageview:iv<br> <br> <br>Imagebutton:im<br> <br> <br>Button:btn<br> <br> <br>Name of view variable in 5.activity<br>Named Mode: logical name +view abbreviation<br> <br> <br>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<br> <br> <br>Name of ID in 6.strings.xml<br>Naming Pattern: Activity name _ Function Module Name _ Logical name/activity Name _ logical Name/common_ logical Name<br> <br> <br>strings.xml, use activity name comments to separate the contents of the file<br> <br> <br>Picture naming in 7.drawable<br>Naming Pattern: Activity name _ logical Name/common_ logical Name<br> <br> <br>8.styles.xml: the continuous style of layout is extracted from the common style generic components, put into the styles.xml;<br>9. Using Layer-list and Selector<br>10. Image as many reusable pictures as possible<br>11. The server can be implemented, do not put on the client<br>12. Refer to Third-party libraries be cautious, avoid the application of large-capacity Third-party libraries, resulting in a very large client package<br>13. Handle the application of global exceptions and errors and send errors to the server by mail<br>14.9 Processing of Pictures.<br>15. Use static variable mode to realize the sharing between interfaces be careful<br>16.Log (system Name Module name Interface name, detailed Description)<br>17. Unit Test (logic test, Interface Test)<br>18. 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<br>Handle all logic in a view.onclicklistener in 19.activity<br>using%1 $ s in 20.strings.xml to implement string wildcard<br>21. 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<br>22. 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<br>23. If you are developing a generic component, to avoid conflicts, add a prefix to the file name in the Drawable/layout/menu/values directory<br>24. data must be tested, such as<br>The character type turns the numeral type, if the conversion fails must have the default value;<br> <br> <br>Whether the service-side response data is effectively judged;<br> <br> <br>25. If the same client is to be placed in a different market, and to be counted in each market to download and use data<br>For different clients to play different packages, the only difference is that the versionname,apk file name is versionname.apk<br> <br> <br>In the upgrade, you want to send their own versioncode and versionname to the server, if you need to upgrade, download the versionname corresponding apk<br> <br> <br>About whether you want to force an upgrade:<br> <br> <br>1). forced escalation in any situation<br> <br> <br>2). determine the User's version and current version, if the compatibility is forced to upgrade, otherwise optional;<br> <br> <br>26. There are buttons to avoid repeated clicks<br>second, Android Performance optimization<br>1.http with gzip compression, set the connection time-out and response timeout<br>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.<br> <br> <br>2.listview Performance Optimization<br>1) Multiplexing Convertview<br>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) {}.<br> <br> <br>2) Loading Pictures asynchronously<br>If the item contains webimage, it is best to load asynchronously<br> <br> <br>3) do not display pictures when sliding quickly<br>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<br> <br> <br>4) the list in the asynchronous loading of the picture, when not in the visual range, according to a certain algorithm in a timely manner (such as in the current visual range of 10 item other than the image to recycle, or to cache the picture, set a size, according to the least recently used principle more than part of the Recycling)<br>5) Baseadapter Avoid memory overflow<br>If the Baseadapter entity class has properties that consume very much memory, it can be saved to a file; for improved performance, you can cache and limit the cache Size.<br> <br> <br>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<br>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<br>1) The main thread does not carry on the network processing;<br>2) The main thread does not do database processing;<br>3) The main thread does not file processing;<br>5. Try to avoid the use of static member variables to reference resources that consume too many instances, such as context<br>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.<br>7. Super Big Fat Bitmap<br>Timely destruction (the activity of the OnDestroy will be bitmap recycled, after being used by the UI components immediately after the collection will throw RuntimeException:Canvas:trying to use a recycled bitmap Android.graphics.Bitmap) set a certain sample rate (the developer provides the picture without sampling, for the user upload or Third-party size of the non-controllable picture, can be sampled to reduce the memory of the image), from the server to return pictures, it is recommended to feedback the size of the picture Clever use of soft reference drawable corresponding to Resid resources, bitmap corresponding to other resources of any type of picture, if not obtained (such as the file does not exist, or read the file run OutOfMemory exception), there should be a corresponding default picture (the default image is placed in the apk, Acquired through resid);<br> <br> <br>8. Ensure that the memory that the cursor occupies is released in a timely manner, rather than waiting for the GC to PROCESS. And Android is obviously prone to programmers manually close the cursor off<br>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<br>10. If the picture of ImageView is from the network, it is loaded asynchronously<br>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<br>12.Drawable<br>The image that the UI component needs to use is the APK package, so use Setimageresource or Setbackgroundresource instead of the ResourceID<br> <br> <br>Note: get (getresources (), R.drawable.btn_achievement_normal) This method is converted to drawable by resid, you need to consider the issue of recycling, if Drawable is an object private object, The memory is definitely not freed until the object is Destroyed.<br> <br> <br>13. Reusing and recovering activity objects<br>Temporary activity in time finish<br> <br> <br>The main interface is set to Singletask<br> <br> <br>General interface set to Singletop<br> <br> <br>14. Location Information<br>When you get the User's geo-location information, turn on the GPS when you need to get the data, and then shut down<br> <br> <br>15. Set the power management of the interface when onresume, and cancel the setting when OnPause<br>third, Androidui Optimization<br>1.layout modular, use merge and include reuse as much as possible<br>2. Using styles, reusing style definitions<br>3. Soft keyboard pop-up control, do not let it overwrite the input box<br>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.<br>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 '<br>6. Complex layout using relativelayout<br>7. Adaptive screen, using DP instead of PIX<br>8. Use Android:layout_weight or tablelayout to make an equal layout<br>9. Animating Effects with Animation-list<br><p><p>Android programming specifications and common techniques</p></p></span>
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