Android activity-start Activity

Source: Internet
Author: User

 

You can call startactivity () to start another activity. When you call this method, you must pass it an intent that describes the activity you want to start as a parameter. This intent can specify the activity you want to start or the description of the action type you want to execute, you can even select from different applications ). Intent can also carry a small amount of data required to start the activity.

When working in your own applications, you often need to simply load a known activity. You can use the class name, create an intent of the activity you want to launch. For example, the following example shows how to start an activity named signinactivity.

Intent intent = new intent (this, signinactivity. Class );

Startactivity (intent );

 

Your application may want to perform actions such as sending an email or text message, or updating the status of data from your activity, your application may not have its own activity that executes these actions. Therefore, you can use the activity provided by other applications on the device to execute these actions for you, this is the true value of intent-you can create an intent that describes the action you want to execute, so that the system will load the response activity from other applications. If there are multiple activities that can process this intent, you can select one of them to use. For example, if you want to allow a user to send an email message, you can create the following intent:

Intent intent = new intent (intent. action_send );

Intent. putextra (intent. extra_email, recipientarray );

Startactivity (intent );

 

The extra_email appended to intent is an array of mail address characters to send mail. When the mail application responds to this intent, it will read the array of characters provided in the attachment, put them in the "to" Field of the mail format. In this case, the email application activity is started. When a user sends an email, your activity is restored.

 

Start an activity for a result

In some cases, you may want to accept the execution results from the activity you started. by calling the startactivityforresult () method, you can start the activity to reach this purpose (instead of startactivity () method ). To accept execution results from subsequent activities, you must implement the onactivityresult () callback method. When subsequent activities are executed, it will return a result to your onactivityresult () in the intent () method.

 

For example, you may want users to select an address book so that your activity can use this address book to do something. The following example shows how to create an intent and process the result:

Private void pickcontack (){

// Create an intent to "pick" a contact, as defined by the content provider URI

Intent intent = new intent (intent. action_pick, contacts. content_uri );

Startactivityforresult (intent, pick_contact_request );

}

 

@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
// If the request went well (OK) and the request was pick_contact_request
If (resultcode = activity. result_ OK & requestcode = pick_contact_request ){

// Perform a query to the contact's content provider for the contact's name
Cursor cursor = getcontentresolver (). Query (data. getdata (),
New String [] {contacts. display_name}, null );
If (cursor. movetofirst () {// true if the cursor is not empty
Int columnindex = cursor. getcolumnindex (contacts. display_name );
String name = cursor. getstring (columnindex );
// Do something with the selected contact's name...
}
}
}

 

This column shows the basic logic that should be used in the onactivityresult () method to process activity results. First, key whether the request result is successful. If the result is successful, resultcode will be result_ OK and determine whether the response result is known to the request (in this example, requestcode and startactivityforresult () method). Next, the Code queries the data returned by intent to process the returned results of the activity.

 

The difference between the query executed by contentresolver and the content is that it returns a cursor that allows data query to read data. For more information, see the content providers documentation.

 

For more information about using intent, see the intents and intent filters (intents and intent filters) documentation.

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.