tags:

views:

1266

answers:

4

Dear friends..

Can any one tell me how to pass the value from one screen to its previous screen. Consider the case.i m having two screen first screen with one Textview and button and the second activity have one edittext and button.

If i click the first button then it has to move to second activity and here user has to type something in the textbox. If he press the button from the second screen then the values from the textbox should move to the first activity and that should be displayed in the first activity textview.

regards, s.kumaran.

+3  A: 

startActivityForResult()

And here's a link from the SDK with more information:

http://developer.android.com/guide/appendix/faq/commontasks.html#opennewscreen

and scroll down to the part titled "Returning a Result from a Screen"

Will
+9  A: 

To capture actions performed on one Activity within another requires three steps.

Launch the secondary Activity (your 'Edit Text' Activity) as a subactivity by using startActivityForResult from your main Activity.

Intent i = new Intent(this,TextEntryActivity.class);    
startActivityForResult(i, STATIC_INTEGER_VALUE);

Within the subactivity, rather than just closing the Activity when a user clicks the button, you need to create a new Intent and include the entered text value in its extras bundle. To pass it back to the parent call setResult before calling finish to close the secondary Activity.

resultIntent = new Intent(null);
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, enteredTextValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();

The final step is in the calling Activity, override onActivityResult to listen for callbacks from the text entry Activity. Get the extra from the returned Intent to get the text value you should be displaying.

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 
  switch(requestCode) { 
    case (STATIC_INTEGER_VALUE) : { 
      if (resultCode == Activity.RESULT_OK) { 
      String newText = data.getStringExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
      // TODO Update your TextView.
      } 
      break; 
    } 
  } 
}
Reto Meier
A: 

Sorry to revive an old thread, but I noticed this and thought it should be clarified.

Is there a reason for using Intent result = new Intent(null); rather than Intent result = new Intent();? The former raises an error saying that the call is ambiguous (there are two Intent constructors that accept a single argument).

Matt
A: 

Yeah now im gettin null pointer exception!! what should I do?? should I call the main_activity.class again in sub_activity using new intent like Intent i = new Intent(this,main_activity.class);

but I thik this is a very bad programming its raises stack beyond as v go on calling both the activities!!???

ashik