Seriously, does your brain even work anymore?
Support our courses! Buy the Pickup Line App!
Let’s catch those extras our main activity is sending. Hop over to our LineActivity java class. Wow, it still exists, this was like 4 pieces of clothing ago.
To get the data that’s coming from MainActivty we do this Bundle, PickupData, getIntent, getExtras. LineActivity knows which Activity opened it, so when we do get Intent it grabs the Intent from that activity, then get Extras gets what’s attached to that intent.
Hmm, but what if we get passed an intent with no data? Say we add another page to our app, but don’t pass data with it.
We can use a conditional to test whether our pickup Data variable is empty or not. Do this with an if statement, so if pickup Data equals to null, then just return. We use equals equals because we are testing if this condition is true not setting a variable
Doing a simple return just stops everything and the Line Activity goes on it’s way unchanged. If we get data from Main Activity we pull that String out of the Pickup Data bundle like this:
Received pickup line, then pickupData, and getString. Then we tell it what string to get by putting in that key of pickup line from the key value pair in the original intents' put Extra method.
After we get it, then it’s a matter of setting the pickup Line text view on Line Activity to a new variable and then running the set Text method on it while passing in the line we just received! Let’s give this a try in the emulator. Awesome! It’s working. We see it change the line to what we set in our Main Activity and overwriting the hardcoded one from the layout.
There is one last thing we need to do on this Line Activity, we need to tell the Rejected button to take us back home. Once again we use it’s ID, retry Button to set it to a new variable, then we set OnClicklistener to that button. For the call back method on this button we’ll put the Activity method, finish. Finish just sends us back to what launched the activity we are on. So our launch page. It’s a little pointless in a two page app when we can use the back button, but this is for demonstration purposes! If we emulate again’ we can click our Clean button, and then send ourselves back to the main Activity with the retry button.
Hope you didn’t finish already too, we’ve got one more to go.
Questions or Comments?