Support our courses! Buy the Pickup Line App!
If I didn't just turn your mind to mush, let’s talk intents.
Intents let activites launch each other, in plain english it’s how we change from screen to screen in Android apps. In our case we want MainActivty to launch our LineActivity AND we want it to send our pickupline along for the ride. To do this make a new method called sendLine outside of our onCreate method. We’ll make it private and it won’t return anything, so "type" is "void". Private means that only this MainActvity class can use this method. Inside this method we’ll create a new instance of the intent class like this, type Intent, the variable, then the new intent, and we pass in the keyword “this”, and the activity we want to launch as Line Activity dot class.
A constructor let's you pass values into a class when you are creating a new object with it.
What the hell is “this”? “This”, is a common keyword used in OOP across many languages. It refers back to the class we’re in, so in this case the MainActity class. So into this new intent, we’re passing the activity we are currently in and the activity we are going to.
Notice that when we create the new intent we get an auto import of a new intent package too. But just creating this new intent object isn’t going to get the job done by itself. We need to run the StartActivity method and pass this intent into it. That’s what will almost get the ball rolling, but still one more step.
See the method name, sendLine, is grayed out?
Whenever you see that it means the method or variable hasn’t been used.
In programming you want to use all the methods and variables you create.
Where do you think we should call this sendLine method if we want it to happen on a button click?
In our callback of course! Let’s go into our callback method for our button, instead of changing the button text, call the sendLine method to send us to the LineActivity. Let’s emulate this sucker! And hot damn, we click the top button and it sends us to our LineActivity.
Of course, the pickup line is still what it was when created our layouts. What if we want to pass the pickup line we set in our Main Acvitiy to our Line Activity?
We can do that in the intent too, use the putExtra method and pass a key value pair. But make sure to add this before StartActivity or your intent won’t get the data. Code in a method is run line by line from the top down.
Let’s say we want to pass the pickup line we defined earlier, type intent, put Extra, with a key value pair of Pickup Line and the variable pickupLine. But, Android studio is a like “NO YOU DIDN’T”.
What’s the problem here? Variable Scope is the problem. That first pickup Line variable was defined within the onCreate method so inside our sendLine curly braces we can’t access it. The curly braces create a separate variable scope. BUT we can pass SendLine as an argument, like this.
Then we can pass the pickupLine into sendLine in the call back method.
In the next lesson we’ll show you how to access this Extra data in the LineActivity class, and if you pass the quiz you can access what’s under this shirt!
Questions or Comments?