Android Best Practices
I have been engaged in android development for more than two years, but my code is not flattering. So I had to improve my Android program to make it more stable and professional. This article is a reference to many ideas from Google. Based on my experience, I have summarized some best practices in Android development. I have accumulated more learning and can make more progress.
Lifecycle of an Android Activity
Lifecycle describes the entire process from creation to extinction of a page on Android. The creation and disappearance of a series of objects cover the business logic contained in the entire page. And interaction with users, accepting user input and returning output information to users.
From its life flow chart, we can see that the life cycle of a single Android Activity is mainly onCreate-> onStart-> onResume-> Activity Running-> onPause-> onStop-> onDestroy. A single App is usually composed of many activities. Generally, a page we see with the naked eye is an Activity, and we often click Button or Back in the APP, so we implement switching between activities.
1. onCreate () and onDestroy ()
How to Understand onCreate? First, let's take a look at the role of the onCreate () method from Google's official explanation:
OnCreate (Bundle) is where you initialize your activity. most importantly, here you will usually call setContentView (int) with a layout resource defining your UI, and using findViewById (int) to retrieve the widgets in that UI that you need to interact with programmatically
Therefore, the onCreate method is mainly used to initialize our Activity. In the onCreate () method, we use setContentView (int) to specify the corresponding View for the current Activity, and then use findViewById (int) to retrieve the components in the current View through id. At this point, some basic initialization work of onCreate () is completed, and the global initialization work of the Activity should be completed in onCreate (). These initialization statuses will be saved until onDestroy () called, which means Activity is extinct.
Observe carefully and find that the onCreate (Bundle) function of Activity has a Bundle type parameter savedInstanceState. The meaningful name shows that this parameter is mainly used to use the previously saved data.
The method corresponding to onCreate () is onDestroy (). The latter is mainly the method that will be executed when the Activity is destroyed. When we call this. finish () manually ends the current Activity. At this time, onDestroy () will be called back. Here we can perform some save operations before the program is destroyed.
2. onStart () and onStop ()
These two statuses usually appear. When you jump from Activity A to Activity B, the onStop function of Activity A is called back, and Activity A is pushed to the stack, and is not visible. Once the user finishes Activity B and enters Activity A again, the system will call the onRestart () and onStart () functions again, so the Activity is awakened again.
3. onResume () and onPause ()
Literally, it means that the Activity is restored and stopped. A typical example is that when the APP is running, there is a sudden call or alarm, so that the current Activity is overwritten by other activities, in this case, the onResume () and onPause () events will be triggered. Of course, if the APP experiences onStart () and onStop () events, it will first pass through onResume () and onPause () event. The life cycle flowchart is shown in. It forms a closed circuit.
With a complete set of Lifecycle of the application, Android ensures that the APP runs well on the mobile phone without interfering with each other. The existence of the Android lifecycle also indicates that in APP development, Activity functions should correspond to each other, some events are registered in onStart (), and anti-registration should be performed in onStop, for example, for GPS Positioning in map development, You need to register in onStart, and do not register in onStop. There are also some similar network access callback situations. We need to pay attention in APP development to ensure that the APP runs in the best state.
Function of the Activity-avoid the Activity class carrying too many functions
As the Controller of Android, Activity directly loads the interaction process with users. During development, Activitiy is easily bloated due to a large number of animation, layout, and other operations. In particular, when the business logic is gradually complicated, the excessive amount of code in the Activity increases the maintenance cost, it is even difficult for developers to understand the true meaning of this Activity. To avoid this situation, we should try to separate the pure UI layout and animation logic into a third-party class. In the interaction process between the Activity and the user, some UI operations and animation performances should be required, you can directly call the relevant UI layout. Our Activity only acts as a real Controller and controls receiving user input and feedback. As for the intermediate logic, it can be completely called by the "Presenter.
Android layout multi-screen adaptation
Because of the large number of devices and resolution, Android sdks customized by different manufacturers also increase the adaptation cost. Therefore, in the development process, we should not use an absolute layout. Instead, we should try our best to use a relative layout. An absolute layout may be good on the current device. If it runs on another machine, layout disorder may occur. During layout, avoid deep nesting between views. Select a proper layout method based on your design drawing. Excessive layout will slow down page rendering speed and affect the performance of the entire APP.
Use Fragment instead of Activity
Since Android4.0, Android has started to use Fragment and is continuously updated. In short, Fragment can also directly replace the Controller of Activity. Fragment is mainly generated to adapt to mobile phones and tablets. Fragment has no difference in performance between mobile phones and activities, however, it can be displayed on a tablet in the form of components, which is more suitable for the screen display mode of the tablet. Fragment allows us not to maintain the two sets of code on both the mobile phone and tablet at the same time, facilitating the development process.
MVC Best Practices
Similar to the MVC Framework in Web development, we can also implement MVC for apps. Specifically, Activity is our Controller, and specific statements such as Data Reading and query should be processed through DataManager, similar to what we usually call Service, the Service does not directly interact with the interface. User interaction occurs with the Activity. The Service provides some data reading methods for the Activity to call.