Android uses startActivityForResult () to obtain the return value of another Activity.

Source: Internet
Author: User

First Activity

[Java]
Package yyy. testandroid5;
 
Import android. app. Activity;
Import android. content. ComponentName;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
 
Public class TestAndroid5Activity extends Activity {
Private EditText edit;
Private Button button;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Edit = (EditText) findViewById (R. id. edit );
Button = (Button) findViewById (R. id. button );

Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
// Open the second Activity
Intent intent = new Intent ();
Intent. setComponent (new ComponentName ("yyy. testandroid6", "yyy. testandroid6.TestAndroid6Activity "));
Intent. setAction (Intent. ACTION_VIEW );
Intent. putExtra ("test", edit. getText (). toString ());
StartActivityForResult (intent, 0 );
}
});

}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = 0 ){
Edit. setText (data. getStringExtra ("return "));
}
}

}
Second Activity

[Java]
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
 
Public class TestAndroid6Activity extends Activity {

Private TextView text;
Private EditText edit;
Private Button button;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Edit = (EditText) findViewById (R. id. edit );
Button = (Button) findViewById (R. id. button );
String str = getIntent (). getStringExtra ("test ");
Edit. setText (str );

Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
// Return value added to the first Activity and set resultCode
Intent intent = new Intent ();
Intent. putExtra ("return", edit. getText (). toString ());
SetResult (RESULT_ OK, intent );
Finish ();
}
});
}
}

 

From the Central-Perk Column

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.