Android App Performance Tuning notes

Source: Internet
Author: User

Java code optimization

Cached results:

If the calculation is too expensive, it is best to cache past results.

The pseudo code is as follows:

Result=cache.get (n); Input parameter N as key

if (result==null)

{

If there is no result value in the cache, it is calculated to fill in

Result=computeresult (n);

Cache.put (N,result); N as the key,

}

return result;

You might want a hashmap to act as a cache, and it can do the job. However, Android defines the Sparsearray class, which is more efficient than hashmap when the key is an integer.

Because HashMap is using the Java.lang.Integer object, Sparsearray uses the base type int. So using HashMap creates many integer objects, and using the

Sparsearray can be avoided.

Api

if (build.version.sdk_int>=build.version_codes. Honeycomb)

{

Sparsearray.removeat (1); API level 11 and above

}

Else

{

int Key=sparsearray.keyat (1); The default implementation is slower

Sparsearray.remove (key);

}

This type of code is very common, and it can use the most appropriate APIs to get the best performance, or it can work on the old platform (which may be using a slower API).

data structure:

If you use a hash-based data structure (for example, HashMap) and the key is a custom object, make sure that you correctly overwrite the equal and hashcode in the class definition

Method. The poor implementation of Hashcode can easily nullify the proceeds of the hash.

Whenever a new version of Android is released, pay special attention to the Android.util package and the Java.util package. (because almost all of the components depend on these two toolboxes).

Ability to respond:

Applications can defer the creation of objects until they are needed, called deferred initialization techniques.

To optimize the startup sequence for activity:

Oncreate->onstart->onresume (This sequence occurs when the activity is created), when the configuration changes, the current activity is destroyed and a new instance is created, calling

The following sequence: Onpause->onstop->ondestory->oncreate->onstart->onresume

The application can specify the Android:configchanges attribute of each activity element in the Mainfest file, allowing it to accept only the configuration changes that it wants to handle. This can cause

Call the activity's onconfigurationchanged () instead of destroying it.

Typically, when an app starts, Strictmode is enabled both when OnCreate () is called.

Sqlite

Using the + operator to concatenate strings is not the most efficient method, while using the StringBuilder object, or calling String.Format, can improve performance.

Android App Performance Tuning notes

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.