Things to pay attention to in Android development

The JNI code of the same application, do not easily change the version compiled by NDK, otherwise there will be many problems (mainly some methods are implemented differently, and the higher version has more stringent code detection), such as r8, there is no problem, but there will be r9 The problem is, this is a big pit;

In Android JNI code, functions with return types and no return values ​​will not report an error when compiled;

The onPause method of the current Activity will execute the onCreate method of the next Activity only after the execution of the onPause method of the current Activity is completed. Therefore, it is not suitable to do time-consuming work in the onPause method, which will affect the efficiency of jumping between pages;

Use Android's transparent theme carefully. Transparent themes can cause many problems. For example, if the new Activity uses a transparent theme, the onStop method of the current Activity will not be called; when you press the Home button on the Activity interface that is set to the transparent theme, it may It will cause the problem of unclean refreshing; entering the interface with a transparent theme will have an obvious sense of delay;

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

The public interface must take into account the situation of code reentry. If it can be designed as a singleton, use a singleton as much as possible;

Do not pass large data through Bundle, otherwise TransactionTooLargeException will be reported: java-Issue: Passing large data to second Activity

Try not to cache data through Application, this is not stable: Do not cache data in Android's Application object!

Try not to use AnimationDrawable. It loads all pictures into the memory when it is initialized. It occupies memory and cannot be released. After release, it will report an error the next time it is loaded again; 9 pictures cannot be compressed by tinypng, otherwise there will be problems ;

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

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

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 for compiling scripts. Configuration is troublesome;

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

The implementation of AsyncTask of 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 multiple AsyncTasks are executed at the same time in the program, it is possible to encounter an AsyncTask excute method for a long time. No implementation. The reason analysis and improvement plan of calling the excute method of AsyncTask can not execute the program immediately

In the same application, the same pictures are placed in drawable-xxhdpi, drawable-xhdpi, drawable-hdpi, drawable-mdpi, and drawable-ldpi. The memory occupied by the same device will be very different (the device’s dpi is fixed, The picture is placed in different dpi folders. When displayed on the device, the picture needs to be converted to the same dpi as the current screen and displayed on the device. Therefore, even if the picture is the same size in different dpi folders, it is stored in the memory. The size is not the same, not necessarily the length and 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 strongly recommend this screen adaptation video tutorial, which can be watched in two and a half hours: Android-Screen Adaptation Guide

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 P263 of "The First Line of Code";

When multiple programs share a set of codes (a set of codes, multiple icons on the desktop), it is necessary to deal with the stack problem when entering different entries;

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

Use Android's multi-process with caution. 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 achieved (if any peers have successfully implemented the complete exit program through multiple processes in the application, please communicate with them);

(2) There will be a delay when entering the page of the new startup process for the first time (there may be a black screen or a white screen for a few seconds, whether the white screen or the black screen is related to the theme of the new Activity);

(3) When there are multiple processes in the application, a newly started process will re-run the Application's onCreate method. It will not re-create an Application, but will re-run the Application's onCreate, so that data cannot be cached in the Application as a way to share memory Up

(4) When sharing data through SharedPreferences between multiple processes is unstable, you can refer to "Exploring Android Development Art" for details.

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 cancelled when the Toast is continuously displayed (If you have continuous pop-up Toast, avoid using Toast.makeText);

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;

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

Regarding the experience of using AS, see: Some issues that need to be understood during the use of Android Studio

After the Eclipse project is converted to the AS version, the installation on the same machine will report the "INSTALL_FAILED_VERSION_DOWNGRADE" error. The reason is because as can set the version name and version number of the apk in the Manifest.xml file, it can also be set in build.gradle Set the version name and version number of the apk in the file, remember to modify the version name and version number in build.gralde to the latest;

Under normal circumstances, after inserting the USB, you may jump to a new interface. At this time, you may have a horizontal screen, but suddenly jump to this new interface is a vertical screen. Although your interface is pressed below, but You will still be forced to switch between horizontal and vertical screens once. If your interface is not processed at this time, it will be overloaded. If there are many fragments in your interface, the overloading at this time is more complicated and difficult to handle. Therefore, it is recommended that the interface that does not switch between horizontal and vertical screens should be changed without overloading.

If you set an activity in the manifest to android:windowSoftInputMode=”adjustResize”, then the ScrollView (or other scalable ViewGroups) will shrink to make room for the soft keyboard. However, if you set android:windowFullscreen=”true” in the theme of the activity, then the ScrollView will not shrink. This is because this property forces the ScrollView to display in full screen. However, setting android:fitsSystemWindows=”false” in the theme will also cause adjustResize to not work;

When doing custom handwriting functions, not all the points reported by the bottom layer can be received in MotionEvent in time. For example, if the bottom layer has 200 points per second, the upper layer may only receive dozens of points. In order to improve the fluency of handwriting, In onTouchEvent, all the points in the process of transferring from the bottom layer to the upper layer can be obtained through getHistorySize in MotionEvent.

Can’t use switch-case statement to access resource ID in Android library: Can’t use switch-case statement to access resource ID in Android library. Reason analysis and solution
can’t display PopupWindow and Dialog when Activity is not fully displayed: popupwindow-Problems creating a Popup Window in Android Activity
Do not 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
Sometimes you cannot use the Application Context, or you will get an error (such as startup) Activity, display Dialog, etc.)
(This article is for transportation and learning)

Guess you like

Origin blog.csdn.net/weixin_44941105/article/details/106979063