Things to keep in mind in Android development

Source: Internet
Author: User

1, the sub-thread can not update the interface, the update interface must be in the main thread

2, Fragment attention to matters:

a) Activity calls the method in Fragment

b) Thread or Handler call method in Fragment

Be aware that getactivity () may be empty; you need to add the following code to avoid null pointers

If(getactivity () = = NULL | |!isadd ()){

Return

}

3. Constants and variables of type String are compared to each other to use the constant equals variable, because the variable may be empty;

If ( constant . Equals ( variable )) {

...

}

4, Activity or Fragment in the broadcastreceiver set monitoring and cancellation of monitoring to appear in pairs. calling external class in OnReceive method to empty

5. Network operation cannot be called in the main thread

6, the activity must destroy the activity of the Dialog before the destruction ; activity cannot occur when destroyed, the Dialog object is also manipulated

7. When using the database, we should pay attention to the problem of database deadlock. If the database does not synchronize, the dbmust be called in the main threadto ensure that the db is not

8. Try Catch cannot use log to print error messages, avoid null pointers. Use e.printstacktrace () to print error messages

9, open the flow resources must be closed, such as file read and write, database read and write

10. static referenced Map or List collection to be released manually, try not to use the static Reference collection

11. You cannot delete a collection when iterating through the collection. Often appear in multiple threads

12, a method can not be too long, the method is too long to split into several methods

13, judge the conditions more than the case, try to use the Guardian statement instead of nested conditional expressions , for example:

If (!file.exist ()) {

Return

}

This avoids too deep a hierarchy, with more focus on key logic when maintaining code, ignoring illegal situations

14 Keep long-lived references to a context-activity (a reference to an activity should has the same life cycle as T He activity itself)

15. Try using the context-application instead of a context-activity

16, Avoid nonstatic inner classes in an activity if you don't control their life cycle,use a static inner class and

Make a weak reference to the activity inside. The solution to the issue are to use a static inner class with

A weakreference to the outer class, as-is-viewroot and its-W inner class for instance.

( use the static inner class when creating the handler , otherwise a memory leak will occur )

17, the Android UI layout in the use of SP units, the other use of DP units, can not use px units

18, a method within the hierarchy too deep, you can use the Guardian statement to reduce the level, you can extract multiple methods, reduce the amount of code

19, use less broadcastreceiver, frequent use of radio will cause

A) Many-to-many calls

b) Transfer parameters are freely uncontrolled

c) Like a pointer, no stack information, the problem is not easy to solve

d) Failure to encapsulate the class, violating the principle of cohesion low coupling

20, can not use Handler case, as far as possible to use less Handler

A) transfer parameters are freely uncontrolled, high maintenance costs

b) After the asynchronous message is received, the interface state needs to be judged, possibly the interface has been destroyed

c) Use Handler, be sure to control the scope of a class, cannot pass Handler to other classes, because it will break the encapsulation of the class

d) Like a pointer, there is no stack information, and the problem is not easy to solve. This is relatively broadcastreceiver Easy, because within a class, it's relatively easy to find.

21, programming to follow the principle of high cohesion, low coupling. To minimize coupling between classes and classes, one class relies less on attributes or methods in other classes

22, image management methods can be used Universal-image-loader

23. Boolean variable prefix using has or is

24. Cannot print log with System.out.println

25, variable name can not 1,2,3,4 ... suffix, to look at the name of the idea

26. If method must have {} package

27. Log tag cannot be ""

28, unified use format and import formats Development

29, the code can not be hard-coded (using the separation from the configuration, or through the way of parameters, or through interface-oriented programming)

InAndroiddevelopment sometimes code in order to facilitate the direct hard-coded, in fact, this is a bad habit, inPCthe platform write code may be used more directly. On mobile platforms, there are sometimes multiple language issues to consider,Androidhas designedgetString ()method from the resourceResfolder to read fromStringstrings, these strings areXMLform saved inValues.xmlfile. Androidplatform To some extent more object-oriented, in addition toJavaitself, still used on the frameMVCsuch aUIseparated from the code logic,UIcan focus on the design of the user interface, code can be a similarJarorMIDLfiles to re-release applications, greatly improving code reuse and maintainability.

The above are hard coding disadvantage, in fact, the benefits are also some, from the individual language and simple procedures have high efficiency and simple design methods, but for the complex project or the proposed MVC method design is more reasonable. Android has a caching mechanism for resource files and Global registration in the R class, meaning that Findviewbyid will The XML file is cached, such as Layout or String.

30. Cannot perform independent database operations in the For loop

31, can not use ScrollView parcel Listview/gridview/expandablelistview; because that would put ListView of all Item are loaded into memory, consuming huge memory and CPU to draw a surface


The above content is based on the past development experience (self and colleagues) to carry out the summary, if there is something wrong also please correct, welcome everyone to put forward their own ideas, the blog I will continue to update ..., I hope we all progress together!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Things to keep in mind in Android development

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.