Android and MVC

Source: Internet
Author: User

activity and the MVC mode of Android
Activity is one of the four components of the Android system and can be used to display the view. Activity is a system module that interacts with the user, and almost all of the activity interacts with the user, but if that's the case, it's not true that the activity is mostly used to show the view, and you know the design pattern that combines Android. Let me start with an introduction to MVC's application in Android development:
M (model): Model is the main part of the application, all business logic should be written here, in Android, the Model layer and Java EE changes little, such as: the operation of the database, the operation of the network and so on the layer (but not that they are in the same package, Can be divided into open, but they are collectively referred to as the model layer).
V (View view): Is the part of the application that is responsible for generating the user interface, but also in the entire MVC architecture of the user can only see a layer, receive user input, display processing results, in the Android application is generally used in the XML file in the German interface description, when used can be very convenient to introduce, Of course, you can use javascript+html and other methods as a view.
C (Controller control layer) the task of Android's control layer will fall on the shoulders of many activities, so it is necessary to suggest that you do not write too much code in the activity, as far as possible to the activity delivery model business Logic layer processing. Well, after introducing the MVC architecture in Android app development, we can clearly know that activity in Android is primarily for control, it can choose which view to display, can get data from view, and then pass the data to the model layer for processing. , and finally show the results of the processing.
After introducing the main role of activity, then we need to talk about the activity in detail. The Activity's life cycle is controlled by the following functions.
public class Activity extends ApplicationContext {
protected void OnCreate (Bundle icicle);
protected void OnStart ();
protected void Onrestart ();
protected void Onresume ();
protected void Onfreeze (Bundle outicicle);
protected void OnPause ();
protected void onStop ();
protected void OnDestroy ();

}

Android System layering :

Linuxkernel: Responsible for the hardware driver, network, power, system security, memory management and other functions.

Libraries and androidruntime:libraries: That is, the C + + function library section, most of which are open source libraries, such as WebKit, which is responsible for the operation of the Android Web browser, such as the standard C function library libc, OpenSSL, SQLite and so on, of course, including support game development 2DSGL and 3dopengles, Multimedia has mediaframework framework to support a variety of audio-visual and graphics file playback and display, such as MPEG4, H. V, MP3, AAC, Numerous multimedia file formats such as AMR, JPG, and PNG. The runtime of Android is responsible for interpreting and executing bytecode in the generated Dalvik format.
Applicationframework (Application software architecture), Java application developers are primarily using this layer of packaged APIs for rapid development.
Applications: This layer is the Java application layer, Android built-in GoogleMaps, e-mail, instant messaging tools, browsers, MP3 players, etc. in this layer, Java developers developed programs are also in this layer, And the built-in application has an equal position, can call the built-in application, can also replace the built-in application.
The above four levels, the lower level for the upper layer of service, the upper layer needs the lower layer of support, call the lower level of service, this strict hierarchical approach brings great stability, flexibility and scalability, so that the different layers of developers can focus on specific layers of development.
The Android application uses the framework's API and runs under the framework, which leads to a high degree of consistency in program development, and on the other hand tells us that it is necessary to have a very deep understanding of the entire applicationframework to write high-quality and efficient programs. By mastering Applicationframework, you can really understand the Android design and operating mechanism, and you will be able to harness the development of the entire application layer.
2,android's official recommendation is that the development of the application takes the MVC pattern. What is MVC? Look first.
MVC is an acronym for Model,view,controller, and you can see that MVC contains three parts:
L Model object: is the main part of the application, and all business logic should be written in that layer.
View object: Is the part of the application that is responsible for generating the user interface. It is also the only layer that the user can see in the entire MVC architecture, receiving input from the user and displaying processing results.
L Controller (Control) object: is based on user input, control user interface data display and update model object state of the part, the controller more important a navigation function, want to use the user to start the related events, to M oh got processing.
Android encourages weak coupling and component reuse, and MVC in Android is embodied in the following:
1) View layer: The general use of XML file interface description, the use of the time can be very convenient to introduce, of course, how you learn more about Android, you can think of Android can also use javascript+ HTML and so on as the view layer, of course, there is a need for the communication between Java and JavaScript, fortunately, Android provides a very convenient communication between them implementation.
2) control layer (Controller): The task of Android control layer usually falls on the shoulders of many acitvity, this phrase also implies do not write code in acitivity, to through activity delivery model business Logic layer processing, Another reason for this is that the acitivity response time in Android is 5s, and if the time-consuming operation is put here, the program is easily recycled, and in the Android world, the controller is usually a subclass of activity, fragment, or service. , data binding in the Android SDK also uses a similar approach to the MVC framework to display the data. Data binding is achieved by encapsulating the data in the control layer as required by the view model (that is, the adapter in the Android SDK) to be displayed directly on the view model. For example, display all the data in the cursor listactivity, its view layer is a ListView, the data encapsulated as ListAdapter, and passed to the ListView, the data is in the ListView reality, Android Controller is very complete, provide activity,service,broadcast and Receiver and so on mechanism.
3) Model: the operation of the database, the operation of the network, etc. should be processed in the model, of course, business calculations and other operations must be placed in the layer.
3, design mode and IOC (control inversion)
There's no doubt that Android's ability to become an open spectacular system is inseparable from the subtle application of design patterns, and as long as you look at it a little bit, you'll find that there's a design pattern or a combination of design patterns everywhere in Android, Design mode is what you need to master Android:
The source of the Android Framework's appeal is the IOC, which, in the course of developing Android, will always feel the tremendous convenience that the IOC brings, and for the activity, the following function is called automatically by the framework call:
protected void OnCreate (bundle savedinstancestate);
It is not the program Writer's initiative to call, but the user-written code is called by the framework, which is reversed! Of course the IOC itself is much more than that, but in this case you can also peep at the great benefits of the IOC. Examples of this class are ubiquitous in Android, such as the management class for databases, such as handler calls for Sax in Android. Sometimes you even need to write a simple IOC implementation yourself

MVC Benefits : From the user's point of view, users can choose their own appropriate way to browse the data according to their own needs. For example, for an online document, you can choose to read it as an HTML page, or you can choose to read it in PDF mode. From the developer's point of view, MVC separates the logic layer of the application from the interface, and the biggest benefit is that the interface designer can directly participate in the interface development, the programmer can focus on the logic layer. Rather than as before, designers put all the material in the hands of developers, who implement the interface. In the Eclipes tool, the development of Android in a more simple way, designers in the DroidDraw design interface, in XML storage, in the Eclipes directly open to see the designer interface.

Http://www.pocketdigi.com/20131223/1231.html

Http://www.tuicool.com/articles/maQNNz


Android and MVC

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.