Android and MVC

Source: Internet
Author: User

Activity and the MVC mode of Android

Design patterns involved in http://blog.csdn.net/dengshengjin2234/article/details/8502097//android

http://www.cppblog.com/weiym/archive/2014/07/26/207819.html//client Architecture Design

Activity is one of the four components of the Android system. Can be used to display view.

Activity is a system module that interacts with the user, and almost all of the activity is interacting with the user. But let's say that the activity is mostly used to show that the view is not quite right. Also know the design patterns combined with Android. I'll start with an introduction to MVC's application in Android development:
M (model): Model is the main part of the application, and 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 placed in the same package, can be divided into open, but they collectively referred to as the model layer).
V (View view): Is the part of the application that is responsible for generating the user interface and the only layer that the user can see in the entire MVC architecture. Receive user input, display processing results, in the Android application is generally used in the XML file in the German interface descriptive narrative, the use of the time can be easily introduced, of course, can also 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 activity. Therefore, it is recommended that you do not write too much code in the activity. As far as possible the activity delivery model business Logic layer processing.

Well, once we've introduced the MVC architecture in Android app development, we'll be able to see it very well. Activity in Android is primarily used for control. It can choose which view to display, can get the data from view and then pass the data to the model layer for processing, and finally show the processing result.
After introducing the main role of activity, then we need to specifically talk about activity.

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, mostly open-source libraries, such as WebKit, the library is responsible for the implementation of the Android Web browser, such as the standard C function library libc, OpenSSL, SQLite and so on. Of course also includes support for game development 2DSGL and 3dopengles. There is a mediaframework framework in multimedia to support the playback and display of various audio-visual and graphic files, such as MPEG4, H, MP3, AAC, AMR, JPG and PNG, and many other multimedia file formats.

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 high-speed development.
Applications: This layer is the application layer of java. Android's built-in GoogleMaps, e-mail, Instant messenger, browser, MP3 player, etc. are at this level, and the Java Developer program is in that layer, and the built-in application has an equal position to invoke the built-in application. can also replace built-in applications.
The above four levels, the lower layer for the upper service. The upper layer needs the support of the lower layer, call the lower-level services, such a strict hierarchical approach brings great stability, flexibility and scalability, so that the different layers of developers can focus on the specific layer of development.
The Android application uses the framework's API and executes it within the framework, which leads to a high degree of consistency in program development, and also tells us that it is important to have a deep understanding of the entire applicationframework in order to write high-quality and efficient programs. Proficient in applicationframework, you can really understand the Android design and execution mechanism, but also to control the development of the entire application layer
2. Android's official recommended application is developed using the MVC pattern. What is MVC? Look first.
MVC is an acronym for Model,view,controller. From the ability to see MVC consists of three parts:
L Model object: is the main part of the application, and all business logic should be written on that layer.
View object: Is the part of the application that is responsible for generating the user interface. is also the only layer that users can see throughout the MVC architecture. Receives input from the user. Displays processing results.
L Controller (Control) object: is based on the user's input, control the user interface data display and update model object state of the part of the controller more important a navigation function, want to use the user to start the relevant events, to M oh got processing.
Android encourages weak coupling and component reuse, and the detailed embodiment of MVC in Android such as the following:
1) View layer: The general use of XML files to describe the interface, when used can be very convenient to introduce, of course, how you know more about Android. will be able to think of in Android can also use javascript+html and other ways as the view layer, of course, there is a need for Java and JavaScript communication between, fortunately. Android provides a convenient communication implementation between them.


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 response time of Acitivity in Android is 5s, assuming time-consuming operations are put here. The program is very easy to be recycled, in the Android world, the controller is typically a subclass of activity, fragment, or service, data binding in the Android SDK. are also used to display data in a similar way to the MVC framework. 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 the listactivity of all the data in the cursor, and its view layer is a ListView. Encapsulates the data as ListAdapter and passes it to the ListView. The data is real in the ListView. Android Controller is very complete, provides activity,service,broadcast and Receiver and so on mechanism.
3) Model: the operation of the database, the operation of the network, etc. should be dealt with in models. Of course, operations such as business calculations must also be placed on that layer.
3, design mode and IOC (control inversion)
No doubt. The reason why Android can become an open spectacular system is inseparable from the subtle application of design patterns, and only if you look at it with a little effort, you'll get a joint use of a design pattern or design pattern everywhere in Android today, Design mode is what you need to master Android:
The source of the Android Framework's appeal is the IOC, which, during the development of Android, you will always feel the great generosity of the IOC, and in the case of activity, the following functions are called by the framework itself:
protected void OnCreate (bundle savedinstancestate);
It is not the program writer who is actively calling. Instead, the user-written code is called by the framework. That's a reversal! Of course the IOC itself is much more than that, but from this example it can also peek into the great advantages that IOC brings. Examples of this kind are ubiquitous in Android, such as the management class for databases. For example, the handler of Sax in Android is called. Sometimes. You even need to write your own simple IOC implementation

MVC Advantages : From the user's point of view, users can according to their own needs, choose their own appropriate way to browse the data. For example, for an online document, users can choose to read as HTML pages, or they can choose to read them in PDF mode. From the developer's point of view, MVC separates the logic layer of the application from the interface completely. The biggest advantage is: interface designers can directly participate in the interface development. The program Ape is able to focus on the logic layer.

And not as it once did. The designer gave all the material to the developer. By the developer to implement the interface. Developing Android in the Eclipes tool takes a simpler approach, with designers designing the interface in DroidDraw and saving it in XML. You can see the designer's interface by opening it directly in the eclipes.

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.