Search code examples
androidandroid-intentmethodssubactivity

How to put EditText info into a Intent variable


I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method

public class EnterDataActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enterdata);

    Button doneButton = (Button) findViewById(R.id.button_done);
    final EditText getData = (EditText) findViewById(R.id.enter_data_here);


    doneButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent result = new Intent();

            result.putExtra(getData);

            setResult(RESULT_OK, result);
            finish(); // Ends sub-activity


            }//ends onClick
        });
    }//ends onCreate void button
}

Solution

  • Well if you are trying to send the EditText that's not possible.

    If you are trying to send the text that are on that EditText that's possible.

    How to do it?

    Declare a String to save the data (You can avoid this step, but that's more clear)

    String mText = getData.getText().toString();
    

    Then you'll use the getExtra() method to send the String to the new Activity

    Intent i = new Intent(this, MyNewActivity.class);
    i.putExtra("text_from_editText", mText);
    startActivity(i);
    

    Then the last step (You don't ask for it, but you'll need it), get the text.

    //onCreate() of the second Activity
    Intent i = getIntent();
    String mText = i.getStringExtra("text_from_editText");