-1

With some basic knowledge in this area, to pass data between multiple Activities i have created Parcelable class.

My two Activities structure looks similar to the below image,

enter image description here

where within the onClick event listener of button1, i have the below code snippet to pass the selectedText (where the value of selectedText is "Header text" which in displayed above the button1)

 ParcelableClass _ParcelableClass = new ParcelableClass(selectedText);
 Intent intent = new Intent(mContext,Editor.class);
 intent.putExtra("_ParcelableClass",ParcelableClass);
 startActivity(intent);

In the ParcelableClass i can read and write the values, so that selectedText can be assigned to the EditText available in the second activity.

where my requirement is to pass the edited text (new Header text) to the first activity when Ok button is selected, based on that selectedText in the first activity should also changed into "new Header Text".

Do i need to created new Intent while click the ok button, if so it will create a new bundle for the first activity. so i just need to change the header value alone, based on the edited text in the second activity.

Joy Rex
  • 608
  • 7
  • 32

1 Answers1

0

You can use startActivityForResult(...) instead of startActivity(...)

Here's a simple example:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

Then from your second activity just set the data you want to return as an extra to the intent and set the result like this:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

And finally, in your first activity you need to override onActivityResult(...). Something like this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result = data.getStringExtra("result");
        }
    }
}
cvetanov
  • 363
  • 1
  • 3
  • 12
  • This is basically a direct copy of the accepted answer from here: http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android. Either add something new, or flag as a duplicate. At the very least, give attribution to the answer where you copied the code from. – Daniel Nugent Aug 10 '15 at 07:57