Google Developing for Android Learning summary

Source: Internet
Author: User

avoid using memory in loopsIt is also understood that to create as few objects as possible in the loop, the custom control avoids frequently creating paint objects inside the OnDraw. avoid memory allocations whenever possibleObject cache:constants are cached by class level or static. Object pool:for multiple objects of the same type, consider using an object pool instead of allocating frequently allocated memory. (not very understanding, still need to study deeply) use ArrayList.Android Collection class:use Arraymap or Simplearraymap instead of HashMap.methods that need to be modified:Use as few object types as possible, but with raw types of data, Android provides Sparseintarray and Sparselongarray collection classes using primitive types instead of object types. use a collection traversal without using iterator for looping, because using iterator causes memory allocations for iterator objects. If it is not to be used, make a non-null judgment. avoid using enumerations, because enumerations are optimized to be int values when Proguard optimizes code and makes code confusion. for using third-party libraries, if possible, extract the code that is useful to you wherever possible and avoid useless third-party code. avoid static variables holding references to activity, as this can cause a memory leak. use Activitymanager.islowramdevice () to apply run-time memory limits. use Intentservice instead of service. Thin-Body your app code. avoid initializing operations in application, instead of methods: use Canliley to manipulate global, public data or methods. avoid the view hierarchy of the responsible layout, which can be used to compose complex view layouts into reusable, low-level controls, such as TitleBar. UI thread reduces time-consuming operations. It is recommended that you use a thread pool for DB and network operations. Avoid time-consuming operations in OnDraw and OnLayout when customizing controls. when you're done, close your broadcast. Avoid affecting the performance and resource consumption of your device due to excessive intent. Network:avoid overly frequent network requests. Because it consumes the power and resources of your phone. network operations are placed in a child thread (typically a thread pool) to operate. It is necessary to check and judge the network status for the operation of network changes. Consideration of poor network environmental issues. network interface Layer-level design, including reasonable requests and JSON data interfaces. Languages and libraries:use Android's own library instead of Java's Library API, such as Arraymap instead of Hashmap,sparsearray. serialization: I generally write Basebean class implementation serialize to implement serialization. The article points out the disadvantages of parcelable serialization:It is not safe to write parcels to the hard disk you can implement your own parcelables, but if you cannot access the same to the class when unparceling (Parcel deserialization), then Unparcel will fail (also applicable for passing parcels to the framework).  The fact that some objects are stored in parcels instead of shared memory, such as file descriptors, may be a good performance optimization, but it hides the real memory cost of the parcel object (until the object is unparceling deserialized before it consumes real memory).  after android5.0, the Persitablebundle class can serialize the data of the bundle subclass, but it does not supportParcelables.  UseParcelables, sqlite,sharedpreferences to avoid excessive serialization.  avoid jni,bug difficult to adjust, multi-platform compilation and other shortcomings. HDD Storage:
Useenvironment.getexternalstoragedirectory ()instead:/sdcardContext.getdatabasepath (), Context.getfilesdir () instead:/data/data/myapp/databases
 persist the relative path instead of the absolute path, because the path has a problem with the path being changed.  temporary files use the Context.getcachedir () cache path.  too simplistic a requirement to use sharepreferences as far as possible to solve without using SQLite to operate.  avoid creating too many db files, this problem rarely occurs with Ormlite,greendao, but if you follow the standard SQLite Android notation, It is possible to create multiple DB files in the Sqliteopenhelper integration class's method of creating db, so be aware of this. Try to achieve multi-table one db. Architecture Layer:use Fragement to implement part of the code functionality to reduce service usage.  Avoid StartService and bindservice two of service operations appear in one logic at the same time.  pass large objects through binder.  Broadcast distribution events, service processing events of long life cycle.  pulls UI processing out of the background service. (e.g. audio, separating playback from view) Useful interfaces:separate the thread pool of the network operation from the locally issued storage thread pool. Ensure that they do not affect each other.  The request and the corresponding data to the necessary cache, the requested data is cached in order to be in a poor network environment, the possible retry operation, and the corresponding data is poor network environment, to avoid experiencing bad network data load, For example, the network image of the level three cache and data cache, etc. (data can be cached in the database through the ORM, the picture utilizes the third party's Piscco and Umloader to carry on the picture cache, avoids does not compare the repetition load, reduces the user traffic waste).  avoid asynchronous time-consuming operations that hold view references for a long time.  properly handle the different use between getapplication and the context of activity, because for example the use of dialog, can not be passed Getapplication interface display. Tools:SystraceAllocationtrackerTraceviewHierarchyviewerMATMemorymonitorOn-deviceSrictmodeProfile Gpu RenderingDebuggpu overdrawAnimator Duration Scaleadb shell Screenrecord/sdcard/myscreenrecord.mp4
Show Hardware layer Updates
but I've only used Mat,traceview, Strctmode, and nothing else . article reference from: http://www.lightskystreet.com/, and added to its own partial understanding. Click the link for more details to seea detailed description of the Lightsky.

Google Developing for Android Learning summary

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.