Article 2 template mode of Android Application Development Mode

Source: Internet
Author: User

Android provides an application framework that contains various services and provides standard application development methods for application developers. application developers only need to use four components, implement related interfaces provided by components and resources used by applications, layout and engineering XML files, and finally package them into an APK file to complete application development.

The android Framework draws on many design ideas of the J2EE framework. The framework provides complete services for service providers. Applications are composed of components and started by components, the Application Component runs in the framework with the help of the framework. The framework is highlighted by the application server and container, providing the runtime environment for the Application Component and controlling the lifecycle of the application component.

How does the android framework control the lifecycle of application components?

It is mainly about the provision of the API of the Initiative framework. The implementation of the API of the Initiative framework is based on the reverse callback (IOC) mechanism (also known as the Hollywood principle. Don't call us. I will call you). The template mode is the core of the entire android framework and adopts the template mode.,The Framework provides hooks for application components to be controlled in their basic components. The Framework calls hooks to control the lifecycle of components. Therefore, the application only needs to re-implement the function overriding in the derived class of the component. This not only enables the customization of the application, but also enables the framework to actively call the application, the purpose of controlling the running and lifecycle of an application.

In this way, diversified applications can be implemented and application development can follow the same framework API. Therefore, providing an API of the active framework is a feature of the android framework and is also widely used in most frameworks, such as the J2EE and net frameworks.

The android framework provides four basic components:

Activity: the user interface component of the android application. fragment is added in Versions later than 3.0. Fragment is affiliated with and dependent on activity. It acts like a "sub activity". It can have its own UI and its own independent lifecycle (add or delete) during activity running ), can receive input events. it can be reused in different activities.

Service: A Background processing component that does not have UI functions.

Broadcast receiver: a component that can be started at any time to process intent and execute its tasks.

Content Provider: A Cross-program data sharing component that provides data sources used by applications.

Activities and services are the main running components. The framework provides the IOC mechanism for the two components and provides the default implementation in the base class. The hooks are called by the framework, by deriving a base class, the application re-implements the hook callback function to customize the application.

The base class of the activity is activity, and the base class of the service is service. Both provide the default Implementation of the hook.

Both are derived from context. The class diagram is as follows:

The bridge mode is used here. contextimpl is the implementation class of the context virtual class and provides functions for interaction with the framework, such as starting additional activities or services, registering broadcast receivers and sending broadcasts, obtain the system service management interface. Contextwrapper is the packaging of the contextimpl class. Both the service class and the activity class are the derived classes of contextwrapper. The service class is directly derived from contextwrapper, and the activity is indirectly derived from contextwrapper subclass contexttheme, the service and activity classes add and implement their own lifecycle callback interfaces (hooks) based on the contextwrapper and contextthemewrapper interfaces ), the function of contextthemewrappe is to add them support on the basis of contextwrapper and perform special processing on the layout_inflater_service service when the getsystemservice interface returns a service management object, returns the cloned object of a layoutinflater object in the original form mode.

Hooks provided by the activity, each of which corresponds to a lifecycle state, oncreate (called when the activity is created), onstart (called when the activity is to be displayed), and onresume (called when the activity is visible ), onpause (the focus of another activity, called when the activity is to be paused), onstop (called when the activity is no longer visible), and ondestroy (called when the activity is to be released ).

Example of callback interface implementation:

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate); // inherits the default implementation

// Add new features here

}

 

 

Full Lifecycle of an activity

However, fragment must implement at least three callbacks:

Oncreate ()

Called when fragmet is created;

Oncreateview ()

Call when fragmet is displayed for the first time. In this callback, bind fragmet to the Layout View and return the Root View corresponding to the fragmet layout. If fragmet does not have a UI, return nulll.

Onpause ()

Called when the user leaves fragmet.

Other callbacks that can be implemented:

Onattach (activity)

Called when fragment is bound to activity

Onactivitycreated ()

Called when oncreate () of the activity returns.

Ondestroyview ()

Called when the view bound to fragment is moved.

Ondetach ()

Called when fragment and activity detach

Ondestroy ()

Called after fragment is released

In addition, the onstart (), onstop (), onresume (), and onpause () interfaces correspond to the status of the bound activity, it is also called when the corresponding status callback interface of the bound activity is called.

The following figure shows the relationship between fragment and the lifecycle of the affiliated activity.

 

The hook callback interfaces provided by the Service include oncreate (called when the service is created) and onstartcommand (called when the service is started by the user through startservice ), onbind (called when the customer calls bindservice to bind to the service, this interface returns a binder object to the customer, and the customer uses this binder object to communicate with the Bound Service ), onunbind (called when the customer calls the unbindservice to bind with the Service), onrebind (called when the customer binds with the service through the unbindservice, and then calls it when the bindservice is re-called ), ondestroy (called when the service is no longer used and released ).

 

 

Service lifecycle Graph

 

The framework provides a virtual base class for the broadcast Receiver component broadcastreceiver and content provider contentprovider components. applications need to implement their derived classes. the broadcast component has only one interface that needs to be implemented in the derived class -- onreceive interface (called by the framework when a message is received ). The content provider component must at least implement the oncreate interface (called by the framework at the time of creation), the query interface (used for data record query) in the derived class ), getType interface (returns the MIME type of the data corresponding to the given URI), update interface (used to update data records), insert interface (inserted data records), delete interface (delete data records ), except that oncreate is called by the framework, these interfaces are called by customers. This is a difference between the content provider component and other components.

You are welcome to repost the document. Please respect the source of the original article.

Related Article

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.