Search code examples
androidonactivityresult

Not getting the text entered in EditText back to the calling activity with on Activity


MainActivity.java - Just having a button and a textview. Button click starts ExplicitlyLoadedActivity having an edittext. User type something and clicks "Enter" button, returns back to MainActivity.java with the text set on the textview of MainActivity.

public class ActivityLoaderActivity extends Activity {

static private final int GET_TEXT_REQUEST_CODE = 1;
public String text = "No Text Entered!";
String requiredValue="a";
// TextView that displays user-entered text from ExplicitlyLoadedActivity runs
private TextView mUserTextView;
Intent in;    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loader_activity);
    // Get reference to the textView
    mUserTextView = (TextView) findViewById(R.id.textView1);


    // Declare and setup Explicit Activation button
Button explicitActivationButton=(Button)findViewById(R.id.explicit_activation_button);
    explicitActivationButton.setOnClickListener(new OnClickListener() {

        // Call startExplicitActivation() when pressed
        @Override
        public void onClick(View v) {

            startExplicitActivation();

        }
    });        
}


// Start the ExplicitlyLoadedActivity

private void startExplicitActivation() {

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

    // TODO - Create a new intent to launch the ExplicitlyLoadedActivity class
    Intent explicitIntent = new Intent();
    explicitIntent.setClass(getApplicationContext(), ExplicitlyLoadedActivity.class);


    // TODO - Start an Activity using that intent and the request code defined above
    startActivityForResult(explicitIntent, GET_TEXT_REQUEST_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 (requestCode == RESULT_OK) {

            requiredValue = data.getStringExtra("text").toString();
            mUserTextView.setText(requiredValue);
        } 
}
}

ExplicitlyLoadedActivity.java

public class ExplicitlyLoadedActivity extends Activity {

private EditText mEditText;
public String s = "abc";
@Override
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()");
    Toast.makeText(ExplicitlyLoadedActivity.this, "Button clicked...",
            Toast.LENGTH_SHORT).show();
    // TODO - Save user provided input from the EditText field
    s = mEditText.getText().toString();
    Toast.makeText(ExplicitlyLoadedActivity.this, "Explicit : " + s,
            Toast.LENGTH_SHORT).show();

    // TODO - Create a new intent and save the input from the EditText field as extra
    Intent i = getIntent(); //I don't know if this is the problem
    i.putExtra("text", s);
    // TODO - Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK, i);

    // TODO - Finish the Activity
    finish();
}
}

Solution

  • You have to build new Intent, replace "Intent i = getIntent()" with - "Intent i= new Intent()"