Android Performance Optimization code example (ii)

Source: Internet
Author: User

1. Use the Maven dependency scheme instead of the import jar package scenario

Suppose you need a third-party jar package in your project. Often the practice is to go online after downloading and then put into the Libs directory, and then add to the project dependencies, but in Android studio has not recommended the use of this set of practices, due to the assumption that the jar has been updated. It's tedious to download the latest version number and then delete the dependency on the history dependency and add the new version number. And in Android studio, this problem using Maven has been very good to conquer, because as the default is the Jcenter Central Library, and jcenter default sync maven Central Library, so we can use the Gradle to join the dependency to replace the previous practice, Like what:

dependencies {    compile ‘com.android.support:appcompat-v7:22.+‘    compile ‘com.squareup.okhttp:okhttp:2.0.+‘    compile ‘com.android.support:recyclerview-v7:22.+‘    compile ‘com.android.support:cardview-v7:22.2.+‘}

We use it this way. A range of version numbers is specified. Take the initiative to get the JAR package resource for the latest version number of the specified range from the MAVEN library when building the project

2, avoid deep-seated layout structure (not more than 5 layers)

Complex structures can be considered with relative layout

3. Extract the same properties of the same control as a style

Like what:
If there are several buttons and the button's style is uniform, then we can extract the button's font size, color, background color, font type, and so on in a style, for multiple button reuse, such as:

<style name="MyButton" parent="Base.Widget.AppCompat.Button " ><Item name="Android: TextSize ">16SP</Item> <Item name="Android: TextColor ">#ffffff</Item> <Item name="Android: Background ">#202020</Item> <Item name="Android: Typeface ">sans</Item></style>

Layout layouts:

<?

XML version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent"android:layout_height="Match_parent" android:gravity="center"android:orientation="vertical"> <buttonstyle="@style/mybutton"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:text=" @string/hello_ World " /> <Spaceandroid:layout_width="Match_parent"android:layout_height ="10DP" /> <buttonstyle="@style/mybutton"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:text=" @string/action_ Settings " /> <Spaceandroid:layout_width="Match_parent"android:layout_height ="10DP" /> <buttonstyle="@style/mybutton"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:text=" @string/app_name " /> </linearlayout>

Effect:

You can see the same styles for these three buttons. Reuse the same style.

4, use Asynctask, using the network request framework (volley, OkHttp) to replace

There is a lot of asynctask on asynchronous network requests because it is lighter to use. However, there are also problems such as memory leak and result loss in Asynctask. Here's a look at:

1. Memory Leaks

Suppose you use Asynctask in activity to request a network in an anonymous inner class. Since the life cycle of the asynctask can be longer than the activity (because it is time consuming to request network data), the Asynctask internal class holds the activity reference, assuming that the activity is closed while the network is being requested. This causes the activity object to be unable to recycle, resulting in a memory leak

2. Loss of results

If the activity's Launchmode is default or standard, then when Asynctask requests the network data to rotate the screen, it will create a new activity again, Again, because the asynctask that is still being executed has a reference to the activity before it, it will cause the OnPostExecute () method to not play any role, the requested data cannot be loaded into the new activity, and will also cause a memory leak

3, serial and parallel multi-version number inconsistent

Asynctask is serial before 1.6, 1.6-2.3 in parallel, and then serial after 3.0. After 3.0, although it is possible to change the default string behavior in parallel by code. But it's a tedious operation.

Android Performance Optimization code example (ii)

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.