What pits should be paid attention to in Android development

As a code farmer with two or three years of Android application development experience, I will naturally encounter many pits. The following are some pits that I can think of (practice has proved that not taking notes is not a good habit), and other pits will be filled one after another when I think of them. superior.

1. Cannot use switch-case statement to access resource ID in Android library: Reason analysis and solution for not using switch-case statement to access resource ID in Android library

 

2. PopupWindow and Dialog cannot be displayed when the Activity is not fully displayed: popupwindow - Problems creating a Popup Window in Android Activity

 

3. Don't use SharedPreferences to share data between multiple processes. Although it can (MODE_MULTI_PROCESS), it is extremely unstable: android - MODE_MULTI_PROCESS for SharedPreferences isn't working

 

4. Sometimes the Context of the Application cannot be used, otherwise an error will be reported (such as starting an Activity, displaying a Dialog, etc.):

 

5. Do not change the version compiled by NDK easily for the JNI code of the same application, otherwise there will be many problems (mainly because the implementation of some methods is different, and the detection of the code is stricter in the higher version), for example, there is no problem in r8, but in r9 There is a problem, this is a big pit;

 

6. In Android's JNI code, functions with return types will not report errors when they are compiled without return values;

 

7. The onCreate method of the next Activity will not be executed until the onPause method of the current Activity is executed, so it is not suitable to do time-consuming work in the onPause method, which will affect the efficiency of jumping between pages;

 

8. Use the transparent theme of Android with caution, the transparent theme will cause many problems, such as: if the new Activity adopts the transparent theme, the onStop method of the current Activity will not be called; when the Home button is pressed in the Activity interface set to the transparent theme , may lead to the problem of unclean screen; Entering the interface with the theme of transparent theme will have a significant sense of delay;

 

9. Do not initialize ViewStub in a non-UI thread, otherwise it will return null;

 

10. The public interface must take into account the situation of code reentrancy. If it can be designed as a single instance, try to use a single instance;

 

11. Do not pass large pieces of data through Bundle, otherwise TransactionTooLargeException will be reported: Java  - Issue: Passing large data to second Activity

 

12. Try not to cache data through Application, this is unstable: don't cache data in Android's Application object!

 

13. Try not to use AnimationDrawable. It loads all the pictures into the memory when it is initialized, which occupies a lot of memory and cannot be released. After the release, it will report an error when it is loaded again next time;

 

14.9 The picture cannot be compressed by tinypng, otherwise there will be problems;

 

15. The genymotion emulator is fast because it is based on the x86 architecture. If you use so in your application, but there is no x86 architecture so, you can only give up using it; the same is true for the Android Studio emulator;

 

16. After the Eclipse Android development environment is configured, do not upgrade ADT and build tools easily, otherwise it will waste a lot of your time, and there should not be too many projects in a workspace, otherwise each startup will be very slow;

 

17. Each version of Android studio and each version of gradle are quite different (I think so). It is recommended to compile jni code in Eclipse. If you develop jni in Android studio, it will waste a lot of time, mainly compiling The configuration of the script is more troublesome;

 

18. Lint in Eclipse is too unreliable, especially when the main project relies on library, many prompts are problematic, it is recommended to use the project cleaning tool of Android Studio, especially recommended;

 

19. The implementation of AsyncTask in different API versions is different. Some can execute multiple tasks at the same time, and some APIs can only execute one thread at the same time. Therefore, when executing multiple AsyncTasks in the program at the same time, it is possible to encounter the execute method of an AsyncTask. It has not been implemented for a long time. Cause analysis and improvement plan of calling the execute method of AsyncTask that cannot execute the program immediately

 

20. In the same application, the same pictures are placed in drawable-xxhdpi, drawable-xhdpi, drawable-hdpi, drawable-mdpi, drawable-ldpi, the memory occupied in the same device will be very different (the dpi of the device is fixed Yes, the picture is placed in a different dpi folder. When displaying on the device, the picture needs to be converted to the same dpi as the current screen and displayed on the device, so even if the picture is the same size in different dpi folders, it is stored in the memory. However, the size of the middle is not the same, not necessarily the length * width * 4). Before doing the memory optimization of the application, you can take a look at how your project is adapted to the screen and whether there is room for optimization. I highly recommend this screen adaptation video tutorial, which can be read in two and a half hours: Android-Screen Adaptation Guide

 

21. Be cautious about database upgrades (such as adding fields to the original database) to avoid data loss or abnormal operation of the database. For database upgrade methods, please refer to "First Line of Code" P263;

 

22. When multiple programs share a set of code (one set of code, multiple icons on the desktop), it is necessary to deal with the stack problem when entering different entrances;

 

23. When using Adapter, if you use ViewHolder for caching, in the getView method, no matter whether each view of this item needs to set properties (for example, the property 2 set by TextView may be null, and the background of a button of item is Transparent, the color of an item is transparent, etc.), you need to set attributes for all views of each item (the attribute of textview is empty and setText("") needs to be set, and the background is transparent also needs to be set), otherwise in the process of sliding There will be confusion in the display of the content.

 

24. Use Android's multi-process carefully. Although multi-process can reduce the memory pressure of the main process, it will encounter the following problems: (1) The function of completely exiting all activities cannot be realized (if some peers use multi-process in the application to successfully implement the (2) There will be a delay when entering the page of the new startup process for the first time (it may be a black screen or a white screen for a few seconds, whether it is a white screen or a black screen is related to the theme of the new Activity); ( 3) When there are multiple processes in the application, when a new process is started, the onCreate method of the Application will be run again, and an Application will not be recreated, but the onCreate of the Application will be run again, so that the data cannot be cached in the Application as a way to share memory. ;(4) It is unstable when sharing data between multiple processes through SharedPreferences. For details, please refer to "Exploration of Android Development Art".

 

25. When using Toast, it is recommended to define a global Toast object, so as to avoid the situation that the last Toast message cannot be canceled when displaying Toast continuously (if you have Toast popping up continuously, avoid using Toast.makeText);

 

26. The larger the area of ​​the View, the longer the drawing time, and the transparent channel has a great influence on the drawing speed of the View;

 

27. Do not pass large objects through Msg, it will cause memory problems.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326274144&siteId=291194637