Android Intents, OOP This, Variable Scope
Hey everyone, soooo it's been awhile we know. This used be something you had to pass the quiz for, but before we release new courses we wanted you to know we're still rockin in the free world. Stay tuned :)
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!
Leave Android Intents, OOP This, Variable Scope to:
Read more #androiddevelopment posts
Best Posts From Apurba
We have not curated any of apurbasikdar82's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Apurba
- These Year-Old Huskies Were Found Abandoned Near A Box Of Food, Toys, And One Heartbreaking Note
- Bitcoin Reached $3300 on Weekends!!
- Arkansas Livestock Cooperative Planning Use Blockchain Technology in Meat Products Monitoring
- Economic Calendar for August 7th: USD/JPY Moved Up After the Last News from Japan
- How To Trade The NFP
- Apple Earnings Report: Is a Beat in Store?
- Everything You Need to Know About the Stock Market
- Bezos Tops Gates as the World’s Richest Person
- Iq Option Live Trades Starting with Only $10 (Hindi) | New Binary Options Strategy 2017
- CSS Properties, Sizes and Colors
- Android Intents, OOP This, Variable Scope
- Bizarre Japanese Drinks
- Liberty Walk Lamborghini Aventador w/ Fi Exhaust
- Hello Guys!! Want to Learn to Code? This is your Solution!
- Giggle Giggle Giggle Giggle
- Technical SEO, Robots.txt, Sitemaps and Boobs
- Pretty Girls
- How To Set Up Doll House
- How to Make a Paper Angry Bird - Very Easy
- How To Make A Paper Butterfly ll Kidzy Sucharita