Android-activity (one of the four components)

Source: Internet
Author: User

First, configure the activity

Android apps require that all components of the application (Activity, Service, ContentProvider, broadcastreceiver) must be displayed in a configuration, as long as <application.../> Element add <Activity.../> child element to configure Activity; typically you need to specify the following properties:

Name: Specifies the class name of the implementation class for the activity;

Icon: Specifies the icons for the activity;

Label: Specifies the tag of the activity;

Exported: Specifies whether the activity is allowed to be called by another application (true allows invocation);

Launchmode: Specifies the loading mode of the activity;

In addition, it is often necessary to specify one or more <intent-filter.../> elements that specify the intent (intent) that the activity responds to.

Second, start, close activity

1. Start activity

An Android application usually contains a lot of activity, but only one activity acts as a portal to the program, and when the application runs it will automatically start and execute the activity. Other activity is usually initiated by the ingress activity or activity initiated by the ingress activity.

Activity initiates other activity in the following two ways:

StartActivity (Intent Intent); initiate other activity;

Startactivityforresult (Intent intent,int requestcode); Initiates activity with the specified request code, and the program will get the results returned by the newly started activity (by overriding the Onactivityresult (...) method to obtain).

2. Close activity

Finish () end the current activity;

finishactivity (int requestcode) ends the activity that starts with the Startactivityforresult (Intent intent,int requestcode) method.

Iii. using bundles to exchange data between activity

When an activity initiates another activity, there is often some data that needs to pass through, which is used to intent, as long as the need to exchange data into the intent. Intent provides multiple overloaded methods to "carry" additional data:

Putextras (Bundle data) into the intent in the need to "carry" the packet;

Bundle Getextras () takes out the packets carried in the intent;

PutExtra (String name,xxx value) deposits data in the form of key-value pairs in intent;

Getxxxextra (String name) extracts the data of the specified type from the intent by key;

Note: When using the Putextra (String name,xxx value) method, if the bundle object is already in the intent object, the method stores data directly into the bundle carried by intent, and if the bundle object is not yet carried, This method creates a bundle for intent and then stores the data in the bundle.

The bundle in the above method is a simple data carrying package that contains several methods to deposit/Remove data:

Putxxx (String key,xxx data) puts various types of data, such as int, long, into the bundle;

Putserializable (String key,serializable data) puts a serializable object into the bundle;

GetXxx (String key) extracts various types of data, such as int, long, from the bundle;

Getserializable (String key,serializable data) extracts a serializable object from the bundle;

During the practice, there is no implementation class found for activity, and the name attribute is written together with the package name in the configuration file as follows:

Code snippet:

Iv. initiating other activity and returning results

V. The callback mechanism of activity

Vi. Activity life Cycle

Vii. Activity Loading mode

Viii. Activity and Servlet

    

  

Android-activity (one of the four components)

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.