0

I am trying to sent the text in an EditText from one activity through an intent extra to another activity. The text will then be used to update a TextView in the second Activity. The EditText activity was invoked using startActivityForResult(). I have the following code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.explicitly_loaded_activity);
    // Get a reference to the EditText field
    mEditText = (EditText) findViewById(R.id.editText);
    // Declare and setup "Enter" button
    Button enterButton = (Button) findViewById(R.id.enter_button);
    enterButton.setOnClickListener(new OnClickListener() {
        // Call enterClicked() when pressed
        @Override
        public void onClick(View v) {
            enterClicked();
        }
    });
}
// Sets result to send back to calling Activity and finishes
private void enterClicked() {
    Log.i(TAG,"Entered enterClicked()");
    // TODO - Save user provided input from the EditText field
    mEditText = (EditText) findViewById(R.id.editText);
    CharSequence userInput = mEditText.getText();
    // TODO - Create a new intent and save the input from the EditText field as an extra
    Intent returnIntent = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
    returnIntent.putExtra("returnInput", userInput);
    // TODO - Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK);
    // TODO - Finish the Activity
    finish();
}

This is then sent back to the following code.

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

    Log.i(TAG, "Entered onActivityResult()");

    // TODO - Process the result only if this method received both a
    // RESULT_OK result code and a recognized request code
    // If so, update the Textview showing the user-entered text.
    if(resultCode == RESULT_OK && requestCode == GET_TEXT_REQUEST_CODE) {
        mUserTextView.setText(data.getCharSequenceExtra("returnInput"));
    }
}

Where mUserTextView is the TextView I want to update. Thanks.

Michael LeVan
  • 528
  • 2
  • 8
  • 21

1 Answers1

1

You did not use the intent that you created in enterClicked()

Change

setResult(RESULT_OK);

to

setResult(RESULT_OK, returnIntent);

and it should work!

You can refer to this link.

Community
  • 1
  • 1
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41