Android: startActivityForResult & setResult for a view class and an activity class

After correcting the other code so that you can run the program, you can retrieve parameters back from your activity colorActivity in this way:

Step1: return some value from colorActivity

Intent resultIntent = new Intent();
resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();

Step 2: collect data from the Main Activity

Overriding @onActivityResult(...).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) { // Please, use a final int instead of hardcoded int value
    if (resultCode == RESULT_OK) {
        String value = (String) data.getExtras().getString("NAME OF THE PARAMETER");

References

Leave a Comment