Six Tips for Android programmers

Source: Internet
Author: User

Six tips for Android programmersCategory: Android related 2015-07-14 23:58 177 people read reviews (0) favorite reports Android Programmer

If you wrote the code a year ago and now you feel like you're doing a good job, you're not learning much.

  1. Do not hold static references in the context

    PublicClassMainactivityExtendslocationmanagingactivity implements actionbar. onnavigationlistener, googleplayservicesclient.connectioncallbacks, googleplayservicesclient. Onconnectionfailedlistener {//... private static metrackerstore mMeTrackerStore; //...  @Override protected void Span class= "Hljs-title" >oncreate (Bundle savedinstancestate) {//... mmetrackerstore = Span class= "Hljs-keyword" >new metrackerstore (this);}      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

    This problem may be a lot of people will make, if MeTrackerStore through activity the construction of the reference, then this will activity never be garbage collection. Unless the static variable is reassigned to a different activity

    This is because it MeTrackerStore is static, and for the storage of static variables, when the application starts and does not collect, it exits the process that the application is running.

    Note: Technically, you can hold a static application that does application context not cause a memory leak, but it is not recommended

  2. Note control the life cycle of the implicit reference object

    PublicClassdefinegeofencefragment extends  Fragment {public class getlatandlongandupdatemapcameraasynctask extends asynctask<string, Void, latlng> {@Override protected latlng doinbackground (String ... params) { Span class= "Hljs-comment" >//... try {//here we make the HTTP request for the place search Suggestio NS HttpResponse = Httpclient.execute (HttpPost); httpentity entity = httpresponse.getentity (); InputStream = Entity.getcontent (); //...} } }} 
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18

    There are a lot of problems with this code, and we put the end point in an implicit reference. In Java, a non-static inner class has an implicit reference to a class that contains a static inner class.

    In this code, a reference to the external class DefineGeofenceFragment will be held GetLatAndLongAndUpdateMapCameraAsyncTask

    For external classes that contain inner classes, they have an implied reference to an instance of the class that contains the anonymous class, which GetLatAndLongAndUpdateMapCameraAsyncTask has an implied reference fragment

  3. Android Studio makes you faster and more comfortable
    As the shortcut keys and live templates, as provided, will let you write code quickly, now Android Studio can support NDK development, there will be more and more people in the future like Andriod studio and use it to develop.

  4. a way to do only one thing
    There is a method in which I write one of the more than 100 lines of the class. Such methods are difficult to read, modify and re-use. Try to write a way to do only one thing. Typically, this means that you should suspect more than 20 lines of code in the method. Here you can recruit the methods of Android studio to help you find the problem.
  5. Learn from people who are more experienced than you
    This may sound trivial, but it's a mistake I made when I wrote my first application.
    When you write an application, you make mistakes. Others have made these mistakes. From the understanding of these people. You are wasting your time if you repeat others ' avoidable mistakes. I wasted a lot of time in my first application so I could have avoided it if I had just spent a little time learning mistakes from experienced software developers.
    Read programmer discipline. Then read the valid java. These two books will help you avoid the mistakes we make when developing newbies. When you do with those books, keep looking for smart people to learn the example.

  6. use third-party libraries
    When you write an app, you may encounter smarter, more experienced people who have solved your needs or problems. Android has a lot of open source libraries and we can use them more.
    In my application, I often use features provided by third-party libraries, such as Retrofit and Picasso. If you're not sure what type or feature library you need, here are some of the more popular libraries:

    1.  Google 广播 (http://fragmentedpodcast.com/episodes/9/) , 告诉你什么情况 使用什么库 , 例如 Dagger Retrofit Picasso 和 Mockito2. 订阅Android周刊 . 他们会给你推送最新的第三方库或比较最新 , 最近比较流行的设计模式及功能.3. 寻找开源代码的应用 去解决类似你需要的功能或模块. 这个应用可能会用到你不知道的第三方库.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Six Tips for Android programmers

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.