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