How to return control to main activity (basic android app)

I'm still quite new to android programming. I currently have a button on the home page that takes the user to a second activity. On the second activity, the user fills in two EditText boxes and clicks a button at the bottom of the page that should send information that the user entered with it. The button that returns to the main activity uses the following code:

Intent returnIntent = new Intent();

    String strData = "your data";

    EditText foodName = ((EditText) findViewById(R.id.foodLabel));
    String foodNameString = foodName.getText().toString();

    returnIntent.putExtra("data", foodNameString);


    setResult(RESULT_OK, returnIntent);        
    finish();

Where would I put code in my main activity that should run as soon as this button is clicked? And how can I access the information I sent back? Right now it returns to the main activity but does nothing.

Upvotes: 0

Views: 198

Answers (1)

Pavel Dudka
Pavel Dudka

Reputation: 20944

If you expect a result from your second activity, you need to start second activity via startActivityForResult() (not the startActivity()). In this case, once activity you launched is finished, Android will trigger onActivityResult() callback within your main activity. All the data you specified via setResult() will be passed in this callback

Upvotes: 1

Related Questions