The pros and cons of some Android class libraries

Source: Internet
Author: User
Tags bulk insert

After my interview experience, as well as contact with the Android project, summed up the advantages and disadvantages of some Android class libraries:

One, threading aspects

1.AsyncTask first is the thread optimization and defects, for most of the current class library, there are good design aspects and defects, such as the internal asynctask, this class advantages, easy to use, accelerate rapid development, but each need new And then put the corresponding parameters in the inside, feel that the process is not very safe, performance needs to be strengthened, mainly the internal multi-threaded single task queue such a mechanism, in fact, very disgusting, as long as most people carefully used this class, can find that he is in fact the order to carry out your task, as long as a little attention Feel like a single-threaded execution, I have to strengthen the design of this class, has not understood why to design, the advantages of where, of course, he also has his good place, is able to customize a thread to the thread pool to carry out the task, can completely abandon his single task of the idea. (it is said that Google engineers say a word: multithreading is a complex thing, we simplify to avoid developers to make mistakes, if you want to multi-threading, call the new method on the line)

2.Vollery This class is Google 2013 to promote a class, he applies to the network communication frequently, a large number of network requests scene, I probably looked at the source code, the implementation mechanism is good, provides the implementation of the cache, followed by a task cancellation mechanism, Ability to cancel some unfinished requests at the end of the activity. But there are also bad places, if only one or two requests, there is no need to pursue this class, because he initializes the time to open several threads, somewhat similar to the Master-woker threading model. Second, in the cache management is not very flexible, in the need to carefully manage the cache, it is not able to subdivide the management of the cache, and finally, when the single data is relatively large, it is best not to use this, such as when downloading files. In fact, why think about it, he will cache the data, if the big data is also cached, in fact, it is quite disgusting.

3. Some internal management of the thread, although multithreading can be used to improve the efficiency and speed of Android, but also bring some negative effects, he will increase the power consumption, and if you do not limit the cost of the thread, may lead to ANR, after all, the thread is to get the time slice to execute, But if a large number of threads are time-consuming, this can also cause some lag. The best way to do this is to unify thread management without casually using the new thread () {}.start (), which is the way to open threads and stay in the two startups feeling that this code is everywhere.

Second, the UI aspect

1. In fact, in the UI adaptation of this aspect, there is a lot of controversy, many people's practice is different folder into different pictures, but this will also have a bad place, he will lead to the whole application becomes very big, recently also pay attention to some cattle people's blog, Stormzhang This said I feel is quite reasonable, In fact, only need to adapt to the large mobile phone pictures, to ensure that the picture as far as possible not to be stretched, in fact, the reduction of the words there is no such a big necessity, and recently with some of the group of Friends in communication, in fact, can also be used to solve the problem vector map, because the vector image will not be distorted, and can also be a good solution to the problem of adaptation.

2. Then there is the layout of the optimization, such as when you use a include this tag, the imported layout often can use the merge this tag, can dynamically replace framelayout, reduce a layer of labels, The two classes about relativelayout and linearlayout have always been replaced, often if your layout is only one layer, then use the LinearLayout, because he is efficient, the layout of the comparison is relatively simple, if the layout hierarchy is complex, Then it's best to use Relativelayout to do the layout. Can effectively improve the efficiency. Most of the time, a visible control is used on the layout, which can be used to improve the viewstub when not in use, and to wait for it to be loaded.

Third, the database aspect

Android database concurrency is not good, because it is not intended for concurrent design, so it is best to use a singleton mode, or multiple threads to operate the database will throw an exception, followed by the batch insert optimization, SQLite by default will add transactions, So when bulk INSERT, you must first turn on transactions in bulk operations in the transaction, otherwise the bulk operation will open multiple transactions, resulting in performance degradation.

Four, the cache aspect

Basically very general, because the cache basically has developed the habit, first of all, the ListView view is cached with Viewholder, followed by the cache of the picture, and the third is the cache of the network, and the next is the use of reasonable caching mechanism to speed up the download speed and communication speed.

Five, latency aspects

This is often used in some of the interface complex and business is also very complex scenarios, for example, many things can be called onstart when the time to load, because OnStart this method call when the user can already see the interface. Can be very good to prevent the interface is too complex to cause the black screen time is too long, but this is only a way to improve the user experience, to root or optimize the interface. The data is then asynchronously loaded. Finally, there is a handler used by the small strategy, when the data is more than the time can be appropriate to delay the handler send, for example, can call handler.sendemptymessagedelayed, set a delay time.

Six, the network aspect

Network optimization has always been a very frequently asked topic, but for the client basically do the optimization of the place is not too much, first guarantee to open gzip compression, two, to the network request plus timeout expiration time, three, the definition of data format, the best way to communicate with Android is JSON, Because of the small amount of data, as much as possible to merge requests, this is a way to improve a lot of efficiency, four, reduce the number of redirects

Seven, code optimization

This is the most common method of optimization in any language, the most fundamental way, the range is too large, can only be used to track with tuning tools. Then to improve the quality of the code, there is no absolute tuning method.

The pros and cons of some Android class libraries

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.