60 technical experiences summarized in daily Android development, android60

Source: Internet
Author: User
Tags try catch

60 technical experiences summarized in daily Android development, android60

1. All activities can be inherited from BaseActivity to facilitate unified style and handling of public events. In the build dialog box, a unified builder is created. In case of overall changes, all modifications are effective everywhere.

2. The field constants in the database table segment are separated from the SQL logic for better clarity. We recommend that you use the Lite series framework LiteOrm library, which is super clear and can focus on the business without worrying about database details.

3. global variables are placed in global classes, and modules are put in their own management classes to make constants clear and centralized.

4. Do not believe in the benefits of a large management class. It may be a disaster. Pay attention to the principle of single responsibility at all times. One class can focus on one thing to make it clearer.

5. If there is no need to load the data, you must delay the initialization of the Data. Remember to save the memory for the user, there will always be no harm.

6. Exception throws are handled in the appropriate location or in a centralized manner. catch is not involved everywhere. It is confusing and has low performance. Try not to catch exceptions in the loop body to improve performance.

7. when the address reference chain is long (more than three points), be careful with Memory leakage and guard against Stack address pointing. A typical event is that the data is updated, but the ListView view is not refreshed, at this time, the Adapter may not point to the data container address you have updated (generally List ).

8. Information Synchronization: For database or network operations, pay attention to the returned ID for newly inserted data (if no unique ID is assigned). Otherwise, no synchronization is performed.

9. When a multi-threaded database is operated, an error may occur when the database is shut down, and mutual lock may also occur. We recommend that you use transactions. We recommend that you use automated LiteOrm database operations.

10. Before doing so, consider those that can be shared, resources, layout, and classes, and perform a structure and Architecture Analysis to accelerate development and improve code reusability.

11. Keep sorting during add and delete operations in the ordered queue; otherwise, you will be embarrassed.

12. When deleting data in a database, pay attention to cascade operations to avoid dirty data that can never be deleted.

13. About the form parameter: when calling a function, the parameter is a value of the basic type, that is, a value, and the parameter is a reference passed by the object, that is, a transfer address.

14. The setSelection function does not work when the data in listview is under one screen. The items in ListView batch operation correspond to the view correctly.

15. Control the amount of Activity code to keep the main logic clear. Other classes comply with the SRP (single function) and ISP (interface isolation) principles.

16. When performing the remove operation in arraylist, pay attention to the difference between int and Integer removal. You know.

17. Mark the Log, and make sure to mark the debugging and printing to locate the printing position. Otherwise, the problem is: I don't know where to print it.

18. code blocks, constants, and resources can be shared in a centralized manner. Even if the sharing logic is slightly more complex, it is worthwhile. It is easy to modify and effective everywhere.

19. setSelection does not work. Try smoothScrollToPosition. The LastVisiblePosition (the last visible sub-item) of ListView changes with the location of the getView method.

20. It is easier to use Handler to communicate with the Activity. If your framework callback chain gets longer, consider the listener mode to simplify the callback.

21. When the listener mode is inconvenient to use, we recommend that you use the EventBus Framework library. If you are not familiar with the time bus, you can make up your mind.

22. Handler transmits mainlogoff to the constructor when the sub-thread uses logoff. prepare or new to ensure the main thread runs.

23. After timepicker clicks OK, clearFocus is required to obtain the time entered manually.

24. Starting asynchronous threads is not recommended in constructor, which may cause hidden risks. For example, if an asynchronous thread calls an example in this example, the thread will crash.

25. Do not take it for granted that an object will not be empty and be fully fault-tolerant. Note that null can be inserted into containers such as ArrayList.

26. The sublist of ExpandableListView cannot be clicked (Disabled). To return the isChildSelectable method of the Adapter to true.

27. Use ScrollView in advance when the UI display is too long. Otherwise, you can understand it on your cell phone.

28. Note that the sensing range of the button is not less than 9mm or not easy to click. In the input box, note that the cursor position is easier for user input.

29. The unique identifier (which may be ID) should be consistent between the server and the client. Otherwise, there may be ambiguity and problems.

30. Comment. Try to write enough comments and describe the ideas to understand the effect of a piece of code.

31. Complete data must use Sqlite's Transaction, and big data must be used. The speed of inserting 100 data records is 20 times faster, and inserting 1000 data records is more than 100 times faster.

32. Avoid the case where String = "null" can be String = null, =. It is a waste of space to submit data such as title = "No topic" to the database.

33. when multiple different dbhelper instances exist, the sqlitedatabase object must have different instances. When multiple threads write data at the same time, db is locked is reported occasionally when data is written in turn, causing a crash, whether you operate on the same table or different table. The read and write operations can be concurrent at the same time and take turns to run irregularly. At the same time, when writing data, the solution is to use transactions for every concurrent thread, but the db will not lock and write data by the whole time.

34. We recommend that you maintain a dbhelper instance for the entire application. As long as the db is not closed, there will be only one db instance globally. Concurrent multi-thread writing to the db will not be locked, and the write will be strictly Alternate: 123123123... (123 indicates that different threads insert a record in turn.) The read and write operations do not lock the database, and there is no rule for switching between read and write operations. The execution count and degree depend on the duration of the thread allocated by the cpu.

35. A task uses a transaction to nest N transactions, and one of the N transactions fails. The entire task fails and data is written only after all the tasks are successful, which is secure and complete. In addition, the efficiency of writing a transaction into a large volume of data is measured by hundreds of times higher than that of a single write. LiteOrm database framework is recommended for massive data and multi-thread operations in databases, which is more stable and simple.

36. You often need to use ListView or other controls that display a large number of Items to track or view information in real time, and want the latest entries to automatically scroll to the visible range. By setting the transcriptMode attribute, You can automatically slide the Android platform control (ScrollBar supported) to the bottom.

37. Long a; determines whether a has a value assignment. if (a = 0), an error is returned if a does not have a value assignment. If (a = null), Integer, Floag, and so on should be the same. You know the reason, just remind you to be careful.

38. The encoding logic, such as reading, writing, and receiving, must be considered in two ways. File Import and Export, and the conversion of character bytes must be transcoded on both sides.

39. The size ratio of an int value to an Integer object (the smallest object that can contain the int value) is about (32-bit and 64-bit machines are different ). The additional overhead comes from the JVM metadata used to describe Java objects, that is, Integer (Long, Double, etc ).

40. The object consists of metadata and data. Metadata includes classes (pointer to a class, describing the class type), tags (describing the object status, such as hash code and shape), and locks (Object synchronization information ). The array object also contains metadata of the size.

41. A Java application that uses 1 GB Java heap during 32-bit Java runtime usually needs 1.7 GB Java heap after it is migrated to a 64-bit Java runtime.

42. The access performance of the Hash set is higher than that of any List, but the cost of each entry is also higher. For access performance reasons, if you are creating a large set (for example, for caching), it is best to use a Hash-based set without additional overhead.

43. List is a reasonable choice for a small set that focuses less on access performance. The performance of the ArrayList and sorted list sets is roughly the same, but the memory usage is completely different: the size of each entry in the ArrayList is much smaller than the sorted list, but it is not accurate to set the size. The correct implementation of List depends on the predictability of List length. If the length is unknown, the correct choice may be the sequence list, because the set contains less space. If the size is known, predictable, or small, the memory overhead of ArrayList is lower.

43. Selecting the correct set type enables you to strike a reasonable balance between the set performance and memory usage. In addition, you can adjust the set size correctly to maximize the filling rate and minimize unused space to minimize memory usage.

44. Fully utilize the encapsulation (provide interface classes to control access to data) and delegate (helper object to implement tasks) concepts.

45. Delayed Hashtable allocation: If Hashtable is empty, it is common to allocate Hashtable only when data needs to be stored. Assign Hashtable to the correct size: Although there will be a default size, we recommend that you use a more accurate initial size.

46. When setText is used, do not forget whether setSelection is required. In most cases, it needs to be set.

47. XML: 1. Duplicate attribute names; 2. Check whether the text contains invalid characters; and use CDATA packages.

48. If there is no obvious logic problem, check whether the object attributes, function parameters, and network transmission parameters are fully understood and set correctly.

49. When a compilation or running error occurs, consider whether your compiling environment and environment version are correct.

50. Because of the immutable nature of the String class, when the String variable needs to change its value frequently, you should consider using StringBuilder to improve performance, and using StringBuffer in multiple threads to operate the string to improve program efficiency.

51. The advantage of java stack is faster and more shareable than the heap, mainly storing temporary variables and parameters. The advantage of the heap is that the memory size can be dynamically allocated.

52. as long as new () is used to create an object, it will be created in the heap, and its data is stored separately, even if it is the same as the data (value) in the stack, it will not share data with the stack.

53. The variables defined in the basic data type are called automatic variables. They are stored in the 'nominal name' and exist in the stack. They can be shared (if they exist, they are not created ).

54. multiple RandomAccessFile objects direct to the same file. You can use multiple threads to write data together without locking the file yourself. The experiment concluded that the three threads write 1 million data respectively, and the lock is used for about 12 seconds, it does not take about 8.5 seconds. It takes about 100 seconds for 10 thousand threads to write 4.2 Data respectively using the lock, and about 3 seconds for no lock to be used.

55. the nextText () method is used with caution in XmlPullParser parsing. xml is complicated and prone to exceptions when there are null tags or duplicate name tags. The getText () method is used in TEXT instead of nextText () in START_TAG () method; START_TAG, TEXT, and END_TAG are used together. Note that TEXT events occur between each xml node (whether the Start Node or the end node.

56. when changing the logic, consider all the places where this function is used. There are more scattered places and it is easy to care about it.

57. when a problem occurs to the native component of the system, view the error stack information, write a subclass of the component, and rewrite the error method in the appropriate place. In addition, try catch ensures that the system will not crash. Do not disturb the normal logic of the system control.

58. Enter the control to control spaces, line breaks, and other symbols. Note the content in the input box and the space of the left and right controls to prevent accidental clicks.

59. Pay attention to the ++ or-operations in the function parameters. Whether it is ++ c or c ++, which is very different.

60. Never underestimate the null pointer in various places, or even kill in some cases.

Q: customized IT education platform, one-to-one service, Q & A. Official Website for developing programming social headlines: www.wenaaa.com

QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!

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.