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:
-
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?
-
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?
-
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.