tags:

views:

1534

answers:

2

I have two activity Main activity and child activity when i press a button the child activity is lunched still now i have no problem.i want to send some data back to the main screen.I used The Bundle class but it is not working.It throw some run time exception is there any solution.Anyone knows the answer please tell me.

+3  A: 

Try calling the child activity Intent using the startActivityForResult() method call

There is an example of this here: developer.android.com/guide/tutorials/notepad/notepad-ex2.html

and in the "Returning a Result from a Screen" of this: developer.android.com/guide/appendix/faq/commontasks.html#implementcallbacks

AshtonBRSC
+7  A: 

There are a couple of ways to achieve what you want, depending on the circumstances.

The most common scenario (which is what yours sounds like) is when a child Activity is used to get user input - such as choosing a contact from a list or entering data in a dialog box. In this case you should use startActivityForResult to launch your child Activity.

This provides a pipeline for sending data back to the main Activity using setResult. The setResult method takes an int result value and an Intent that is passed back to the calling Activity.

Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
finish();

To access the returned data in the calling Activity override onActivityResult. The requestCode corresponds to the integer passed in in the startActivityForResult call, while the resultCode and data Intent are returned from the child Activity.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch(requestCode) {
    case (MY_CHILD_ACTIVITY) : {
      if (resultCode == Activity.RESULT_OK) 
        // TODO Extract the data returned from the child Activity.
      }
      break;
    } 
  }
}
Reto Meier