Reputation: 1068
I'm trying to set the text when I press enter on the first screen so it looks like the second image. Whenever I try to set text nothing shows up on the second screen. I've partially taken out some of the code in the first activity because they are irrelevant.
My first activity
public class ActivityLoaderActivity extends Activity {
static private final int GET_TEXT_REQUEST_CODE = 1;
private void startExplicitActivation() {
Log.i(TAG,"Entered startExplicitActivation()");
Intent explicitIntent = null;
explicitIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
startActivityForResult(explicitIntent, GET_TEXT_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
if (requestCode == GET_TEXT_REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
String value = (String) data.getExtras().getString("userInputMessage");
mUserTextView.setText(value);
}
}
}
}
My second activity
public class ExplicitlyLoadedActivity extends Activity {
static private final String TAG = "Lab-Intents";
private EditText mEditText;
@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()");
//Save user provided input from the EditText field
mEditText.toString();
Intent resultIntent = new Intent();
resultIntent.putExtra("userInputMessage ", "mEditText" );
setResult(RESULT_OK, resultIntent);
finish();
}
}
Upvotes: 1
Views: 170
Reputation: 6679
One obvious issue is that you are setting the string content to the literal "mEditText"
instead of mEditText.getText().toString()
, which would have the actual contents of the EditText
:
resultIntent.putExtra("userInputMessage ", "mEditText" );
And then:
String value = (String) data.getExtras().getString("userInputMessage");
The next issue is that there is a space after userInputMessage
in the first call, so that won't match when you try to get the extra data back later.
Better would be to define that as a public static final String
value somewhere and use the reference in both places.
Upvotes: 2