Previous Blog Portal: Android FAQs Summary (ii)
A. Android Data storage format
There are several main forms of data storage in Android: Sharedpreferrencessharedpreferrences is primarily used to store a small amount of simple application configuration information. Sharedpreferrences stores data on an XML file in the form of a plaintext key-value pair, which is located in the/data/data/<package name>/shared_prefs directory. Therefore, sharedpreferrences is only suitable for storing some simple data and is not suitable for storing complex or sensitive data.
Fileandroid, like Java, also supports the use of file streams to save and access files. In addition to storing files on the phone's built-in storage, Android also supports the ability to read and write files on SD cards: Once the appropriate permissions have been obtained, the path can be obtained by calling environment's getExternalStorageDirectory method.
The SQLite database Android system integrates a lightweight database: SQLite. SQLite is an embedded database engine that is specifically designed to access the right amount of data on a resource-constrained device. On Android We generally use the Sqliteopenhelper helper class to manipulate the SQLite database.
Basic operation of SQLite
SQLite provides the appropriate functions for programmers familiar with SQL statements using SQL statements, as well as simple additions and deletions to programmers who do not understand SQL syntax:
String Path = "Database path"; Sqlitedatabase database = sqlitedatabase.openorcreatedatabase (path, null);//Execute SQL Statement database.execsql (SQL);// Execute SQL statement with placeholder database.execsql (SQL, Bindargs),//SQL statement database.rawquery (SQL, Selectionargs) to perform the lookup;// Perform additions and deletions database.insert (table, Nullcolumnhack, values);d atabase.delete (table, Whereclause, Whereargs);d atabase.query (table, columns, selection, Selectionargs, GroupBy, having, by-and-by);d atabase.update (table, values, Whereclause, Whereargs);//Open transaction database.begintransaction ();//Confirm Transaction Success database.settransactionsuccessful ();// End transaction database.endtransaction ();
The MVC pattern in Android
The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction.
The MVC pattern is as follows:
(Image from Baidu Encyclopedia)
The Android design also uses the MVC pattern, where:
- View the interface that corresponds to the view control and XML written by Android
- Control corresponds to activity in Android and is responsible for receiving user requests and changing the state of the model
- Model is generally implemented by us to handle program functions and logic
The role of Merge and Viewstub
Both the merge and viewstub are XML optimized tags in android for optimizing the view layout of Android. Mergemerge tags are applied to the top-level tags of XML, mainly for the layout of the phenomenon of nesting waste. In an Android layout file, you need a top-level container to accommodate the other components, rather than placing multiple components directly, and by using the merge tag as the top-level container, we can prune extra or extra layers to optimize the structure of the entire Android layout.
Here is an example: Activity_merge_test.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent "> <textview android:id= "@+id/text_view" android:layout_width= "match_parent" android:layout_height= "Match_ Parent " android:gravity=" top|center_horizontal " android:text=" Text_view "/> <textview Android:id= "@+id/text_view_2" android:layout_width= "match_parent" android:layout_height= "Match_ Parent " android:gravity=" bottom|center_horizontal " android:text=" text_view_2 "/> </ Framelayout>
Mergetestactivity:
public class Mergetestactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_merge_test);}}
As follows:
Available from Hierarchyviewer analysis (only the parts related to the activity interface are analyzed here):
The view tree image obtained by hierarchyviewer we can find that only one element of the framelayout in the ID content is linearlayout, resulting in a nested waste of layout (as shown in the red box). If we use the merge tag here, we can reduce the nesting of a layer of layout tags, which makes the program run faster and smoother.
Change the XML code to read as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><merge xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" > <textview Android:id= "@+id/text_view" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:gravity= "Top|center_horizontal" android:text= "Text_view"/> <textview android:id = "@+id/text_view_2" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:gravity= "Bottom|center_horizontal" android:text= "text_view_2"/> </merge>
Without change, using hierarchyviewer analysis can be done by:
At this point, a layer of layout nesting has been reduced, and we have achieved the optimization goal by using the merge tag.
The viewstubviewstub is a placeholder that handles situations in which a view is dynamically felt to be displayed. When developing an application, we often dynamically determine which view or layout to display at runtime, depending on the condition. The most common idea is to write the view that is likely to be used on it, first to set their visibility to view.gone, and then dynamically change its visibility in the code. The advantage of this approach is that logic is simple and control is more flexible. But its disadvantage is that it consumes resources. Although the view is initially visible view.gone but in the inflate layout, the view will still be inflate, that is, the object will still be created, instantiated, and set properties. In other words, it consumes resources such as memory. The use of viewstub words in the inflate layout will not be inflate,viewstub inflate operation is delayed until we call its inflate method.
The XML file for Viewstub is set as follows:
<viewstub android:id= "@+id/view_stub" android:inflatedid= "@+id/my_view" android:layout_width= " Wrap_content " android:layout_height=" wrap_content " android:layout=" @layout/view_view_stub "/>
Where the Inflateid property represents the ID value that viewstub is re-assigned after being inflate, the layout property specifies the specific layout inflate when the inflate method is called.
Locate Viewstub and call inflate:
Viewstub viewstub = (viewstub) Findviewbyid (r.id.view_stub); Viewstub.inflate ();
It is worth noting that:
- Viewstub can only be inflate once, and then viewstub objects will be empty. According to the sentence, a viewstub specified by the layout is inflate, it will not be enough to control it through the viewstub.
- Viewstub can only be used to inflate a layout file, not a specific view.
What are the advantages and disadvantages of JSON?
JSON, or JavaScript Object Notation, is a lightweight data interchange format that has good readability and quick-to-write features. The industry's mainstream technology provides a complete solution (somewhat like regular expressions, which is supported in most languages today), enabling data exchange between different platforms. JSON uses a high-compatibility text format, and also has a similar behavior to the C language system. These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (typically used to improve network transfer rates).
JSON Pros: JSON has some advantages over traditional XML:
- In the decoding difficulty aspect, the XML parsing must consider the child node parent node, but the JSON does not need, the parsing difficulty is low
- Small size of JSON data
- JSON travels faster than XML
But JSON also has its shortcomings:
- There is no XML format so popularized and widely used, without XML so universal
- Namespaces allow the same pieces of information in different contexts to blend with each other, but namespaces are not found in JSON
Android FAQs Summary (iii)