Confused on how we work startActivityForResult

I understand the first intent we used which was simply using startActivity() and passing in some data to the Cheat Activity.

But when we start getting into retrieving data back from Cheat Activity, things get confusing real fast.

Let’s take things in chronological order:

We replace startActivity() for startActivityForResult(intent, REQUEST_CODE_CHEAT);

From Cheat Activity, we pass data back through an intent

private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}

We create this method in the Cheat Activity which I don’t know its purpose.

public static boolean wasAnswerShown(Intent result) {
return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
}

We then retrieve the data from Cheat Activity through this method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_CODE_CHEAT) {
if (data == null) {
return;
}
mIsCheater = CheatActivity.wasAnswerShown(data);
}
}

Here’s what I don’t fully understand:

  1. How does the main activity know the REQUEST_CODE_CHEAT if we didn’t pass that code into the intent that we created from cheat activity when passing data back to main activity. Does it simply know the code because we used that code when we called onActivityForResult() and so any intents sent back from that activity has REQUEST_CODE_CHEAT attached to it?

  2. What’s the point of setResult()? In the book it said it’s not necessary and it’s for when the user pressed OK or CANCEL in some situations. Could we omit this line in our intent from cheat activity?

  3. Why did we throw this method into cheat activity ?

public static boolean wasAnswerShown(Intent result) {
return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
}

I can see it’s being used in the override on onActivityResult() when trying to retrieve data back. But couldn’t you just go

mIsCheater = data.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);

I don’t understand the purpose of putting this into a separate method into Cheat Activity.

  1. This is correct. The request code used in callActivityForResult() is then sent back to the calling activity in onActivityResult(). This request code is strictly for identifying the callActivityForResult() call that the current result came from. As such, there is no need for the called activity to know about it.

  2. setResult() sets the data intent and result code sent back to onActivityResult(). Omitting this line will always tell the calling activity that the user cancelled out of the activity without cheating.

  3. This method allows CheatActivity to hide information about how the cheat flag is stored in the data intent. This can be useful because you can change how the data intent is organized later without breaking code outside the class. In this case, it is probably unnecessary. If you do read the data intent directly in MainActivity, you will need to make CheatActivity.EXTRA_ANSWER_SHOWN public (or at least package-private).

1 Like