How can I pass values between Activities on Android?

Umesh shows a good technique but I think you want the opposite direction.

Step 1

When starting Activity 2 and 3, use startActivityForResult. This allows you handle the result in the calling activity.

startActivityForResult(MY_REQUEST_ID);

Step 2

In Activities 2 and 3, call setResult(int, Intent) to return a value:

Intent resultData = new Intent();
resultData.putExtra("valueName", "valueData");
setResult(Activity.RESULT_OK, resultData);
finish();

Step 3

In your calling activty, implement onActivityResult and get the data:

protected void onActivityResult(int requestCode, int resultCode,
          Intent data) {
      if (requestCode == MY_REQUEST_ID) {
          if (resultCode == RESULT_OK) {
            String myValue = data.getStringExtra("valueName"); 
            // use 'myValue' return value here
          }
      }
}

Edit:

Technique #2

Yes, you can also use global application state by adding a class to your application that extends Application, see this StackOverflow answer

Leave a Comment