Android Program Development: (2) Intent-2.3 returned results from Intent

Source: Internet
Author: User

The startActivity () method can call another Activity, but this method does not return a result for the current Activity. For example, if you have an Activity that prompts the user to enter the user name and password, and the information entered by the user needs to be "Returned" to the Activity, you need to use the startActivityForResult () method.
1. Code in secondactivity. xml.
 
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "This is the Second Activity! "/>
 
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Please enter your name"/>
 
<EditText
Android: id = "@ + id/txt_username"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
 
<Button
Android: id = "@ + id/btn_ OK"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onClick"
Android: text = "OK"/>
 
</LinearLayout>
2. Code in SecondActivity. java. Package net. horsttnann. UsingIntent;
 
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. EditText;
 
Public class SecondActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. secondactivity );
}
 
Public void onClick (View view ){
Intent data = new Intent ();
 
// --- Get the EditText view ---
EditText txt_username = (EditText) findViewById(R.id.txt _ username );
 
// --- Set the data to pass back ---
Data. setData (Uri. parse (txt_username.getText (). toString ()));
SetResult (RESULT_ OK, data );
 
// --- Closes the activity ---
Finish ();
}
}
3. The code in UsingIntentActivity. java. Package net. horsttnann. UsingIntent;
 
 
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Toast;
 
 
Public class UsingIntentActivity extends Activity {
Int request_Code = 1;
 
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
 
 
Public void onClick (View view ){
// StartActivity (new Intent ("net. horsttnann. SecondActivity "));
// Or
// StartActivity (new Intent (this, SecondActivity. class ));
 
 
StartActivityForResult (new Intent ("net. horsttnann. SecondActivity "),
Request_Code );
}
 
 
Public void onActivityResult (int requestCode, int resultCode, Intent data ){
If (requestCode = request_Code ){
If (resultCode = RESULT_ OK ){
Toast. makeText (this, data. getData (). toString (),
Toast. LENGTH_SHORT). show ();
}
}
}
}
4. debug by F11.
 
 
 
:
 
The program is loaded for the first time:
 
 

Click the button and use startActivityForResult () to call SecondActivity:


Click OK to return to UsingIntentActivity:

Www.2cto.com

 
Written by manoel

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.