 Hey everybody, this is Brian and welcome to the third Android tutorial. Today we're going to talk about intense. I wanted to say that like really intensely, but I just failed. Anyways, what is an intent? Very simply, it is an abstract description of an operation to be performed. Well, that makes no sense. Well, if you look out on images, you'll see this beautiful little flow chart. You have activity A and you want Android to do something. Let's say you want to launch another activity, meaning you intend to do something. That's why it's called an intent. You intend to do something. So you take that intention or intent, shove it over to the Android system. The Android system says, yeah, I'll allow it. It takes the intent and then actually creates the activity. So you take your intentions and then put it into an activity. The activity then can fire information back. So that's what we're really going to explore today is how to play around with activities and intents. And hopefully this video doesn't end up being like extremely long because I'm actually kind of hungry. Let's call this app three. And we're just going to do that and empty and finish. And it'll compute the rotation of the earth or do whatever. I don't know. All right. So there we go. We have our lamp and here's our activity. And as we discussed in the previous tutorial, that this will be our default activity through the manifest. You can see we have a main and a launcher here. So here we go. We're going to get rid of that just because I don't want it. And we're going to put some buttons on here. And we're going to see how much of this I can do from memory here. So we're going to say btn start already failed. All right. So update usages. Yes. And we're going to say btn result. And let's call this results. So we have two buttons here start and result. And what we're going to do is we're going to start another activity when we click these things. The difference is going to be start. We'll just start the activity and we don't care what it does. Result will start the activity. But then we want to know what it did. We want some sort of feedback from that thing. So we're going to actually go into our app and make yet a new activity. And let's just make an empty activity here. And let's call this I don't know what to call this customer. Why not? I'm hungry. So my brain is like fried. So we have our customer activity now and see how it's just a blank activity not to be confused with the one that we have our main activity. And on here we are going to drag and drop a button. We'll call this btn close close. So just very simply and we'll just let's just say feed me because I'm like starving. My gosh I wasn't really hungry until like two minutes before I sat down to this video but I really want to do this. So feed me kind of weird but whatever. So when we look at our manifest now you see we have a new activity here. Well we need to tell Android what to do with that activity. So we're just going to grab this intent filter. Remember in the last tutorial I said you could take an intent and filter it and that's what we're going to do here. So customer and we're going to call this default. What's the difference between main launcher and customer default? Well customer is following the name of the customer. We're saying this is an Android intent action is customer. Think of this as like a I don't want to call it a namespace or a class path. I forget while I'm on the spot making the video I forget what it's actually called but it's like a fully quantified name. Think of it like a web address almost. It's a location within your program. Then we have category default meaning it's not a launcher it's just a default activity it's nothing special. We're going to close that out. Now we're going to write some code here and I'm really particular about how these are set up. So we're going to say I cannot type tonight either. Private static button btn start btn results. So we got our two buttons and we are going to wire these things up and have them actually do something. So let's go private avoid on start clicked and we're going to say on result clicked. Now there's an easier way to do this but I like kind of doing it the old school way. The easier way is so you guys can go out and google this is you actually select your button and you go out here and then there is a I think it's on click or something you can add a little element but then it's got to have special properties and things and it was actually kind of confusing. I just like doing it the old way so we're going to actually take this and we're going to go btn start equal and we're going to cast this to a button and we're going to call something called find view by id and this takes a little bit of explanation find view by id when you look at your activity you have your activity and then you have views so pretty much anything inside of an activity is a view so a buttons a view a text boxes a view a check boxes a view a spinner blah blah blah they're called views why that's what google decided to name them so what we want to do is find a view a button by its id well we need to take the r variable which we discussed in the last tutorial we're gonna say id and we're gonna say button start what is this effectively doing we're taking our private static button variable and we're saying cast a button from find view by id so we're taking that xml file searching through this xml file and looking for the variable that android has created when android loads this xml file it creates that r variable and all the properties within the r variable so there's a subcategory and they're called id and then our buttons in there and you can tell that by looking at this where it says android id and we have this at plus id slash btn start there's your actual id or if you're in the designer you just click here and there's your id right here at the top so why does android do it this way that is a very good question this has been a source of frustration for me since the day i started there's probably a better way of doing this my guess is it saves memory and that's the story i'm going to stick with because it's been very frustrating for me all right so btn result so now that we have our buttons we're gonna say btn start set on click listener new on click listener and if you don't know what a listener is i really suggest that you go back and do some basic java tutorials and then we're gonna say btn result i will explain it here in just a second but new on click listener um java doesn't really have um i'm gonna get a lot of hate mail for this but it doesn't really have a very robust event system so you have to use classes and inner classes and all this other gobbledygook that in some languages it's just a one-line thing but in java it ends up being this 500 page monstrosity um so what you have to do is set a listener which creates this anonymous i think that's called an anonymous internal class somebody correct me if i'm wrong this is the internet so i'm sure a thousand people correct me but i think it's called an anonymous inner class so you're creating a new class called an on click listener and then it's in here i mean it's not actually declared as a variable it's anonymous you get the point um but that is how you do that so we're wiring that up now i want to actually create another class here new java class and we'll call this app settings and inside of app settings i want to say public static final string tag equal let's avoid realms what we're going to do now is we're going here we're going to say log dot d and we're going to say app settings tag and we're going to say on star clicked copy and paste this on result clicked so now we have our two buttons and let's just run this make sure everything runs as we expect it will ooh and we are not filtering so let's filter some of this nonsense out here make sure this starts as we expect it in the emulator which will take just a second here now i'm going to go in here and i'm going to actually filter this and that's the actual log tag the log tag we put in app settings as our static there we go what was that must have been another thing that was running whoops got all confused there all right so now when i click you can see on click on result clicked so we have our two buttons wired up so what we're going to do now is we're going to clear that stop our app and we're going to go into this customer what do we call this button close so we're going to go into customer and do pretty much the exact same thing we're going to say private static button close whoops probably help if i gave it a type huh i've been working in python a lot lately so i've been really forgetting to give things types now you notice how it says hey what is this cannot resolve symbol button so you hover over here alt enter and it automatically adds the import for you so that's a lifesaver and you also notice when you copy and paste things in it'll say hey it looks like you're trying to do something and it'll try to import those for you so we're going to say button close equal button and of course we're going to do find view by id and we're going to say r dot id maybe beaten button close maybe there we go and we're going to say private void on those clicked why do i do it this way it's just my preferred method like i said there's a million different ways you can do this um and we're going to say button close set on click listener new on click listener there we go and we're going to say click closed boom and then we're going to say that now that right there is exactly why i moved our tag into a separate class so it's callable all over the place um i could have actually done it main activity done main activity dot tag but it's just the way i do things so now we've got our two little guys here and let's add in a finish here so we got start we got result clicked now what we need to do is we need to tell android that we intend to do something so we need to make an intent so we're going to say intent intent and what we need to do now is tell it what we intend to do so i'm going to go back into the manifest and i'm going to get this big long string here and say this is what we intend to do and there's actually a lot of engineering that went into that it's actually kind of elegant when you think about it so there's the intent we're telling it exactly what we're intending to do which is to open that thing um to do that we do start activity and then we give it the intent now intents can be used for just everything i actually use them for like uh wiring up my own events and things like that so in my my listeners that i make in my classes i'll send an intent right back and you'll see why so we're going to make a private static int and we're going to call this um feed customer equal one you should probably make that final now we're going to go start activity for result in our result clicked now you notice how this has got a little bit of a different syntax um it wants to know let me bring it back up here of course it's not going to do it now it wants to know two different things it wants to know what you intend to do so we're going to give it an intent we're actually going to make the same intent sorry and it wants to know why you're doing it so we're going to start activity for result we're saying what our intentions are and basically what we're trying to do here um the reason for that is we're going to do a control oh we need to get a on activity result so on activity result has got three things it's got a request code a result code and an intent there's that intent again so what we're going to do is we're going to launch this same activity two different ways and you're going to see exactly what happens and we're going to actually just log app settings tag and we're going to say well let's just copy and paste this my brain is just like completely fried i'm tired hungry oops i told you my brain was really fried there we go and let's look at our emulator it's not running good so we're going to fire this bad boy up here and see what we can do all right so here's our activity and we're going to click start feed me now if i click back notice how nothing happens i can even start this and i can click close nothing happens it just says we closed it but it's not firing this on activity result which is what we want to do so when you start an activity in the start clicked on start click sorry um you're just starting it you're just firing and forget you don't care what happens but when you click on the result button notice how we're doing start activity for result meaning we expect a result we demand a result so let's clear this out let's go back here whoops uh we're going to click result same thing shows the activity now when we close it you see on activity result meaning it fired this guy back in our main we can even go here and we can just back out of it and it still gets the on activity result so we've got some rudimentary feedback here that the activity that we've started has now stopped now what we want to do is figure out what happened in that activity um because we're going to do something the customer is going to say i love it i hate it go away bring me water something but we want to know what happened so we're going to go in here and we're just going to play around with this a little bit and we're going to say log dot d app settings dots tag let me close this little thing down so we can see what we're doing here and i'm going to say and we're going to i'm going to actually intentionally crash this program just to show you how to use the debugger so bear with me here you're kind of getting a two for one combo i was going to do like a whole series on debugging but i kind of want to introduce it early um that way if somebody has a problem they know what to do um so we're going to run our app here and we're going to start and we're going to actually crash this thing are you ready three two boom uh but didn't do it oh now i'm sad hmm oh i know i didn't crash hold on a second we will make this thing crash i will crash this we're going to actually do an intent here so we've got our intent and we're going to put some extra data in here so we're going to say put extra and the syntax for this of course is you can give it a name notice how it's string name and then a type so we're going to give it um we're going to say food and somebody somebody anybody what are we eating tonight pizza why not everybody loves pizza so we're going to put an extra and we're going to give it pizza now what we want to do here is go back into customer and before this finish we want to actually tell it hey there was a result of some kind so we're going to say set result and we're going to say result okay notice how there's result cancelled result first user etc etc um we're just going to say result okay now typically this is when you would save some information to do save save save and you would persist that down to a file or database or something like that you can actually even put it in an intent and send it back to the other activity um but let's see if we can crash this sorry i got sidetracked here it probably still won't crash it let me do it again let's say uh has extra food this should crash it if not i'll be surprised so we're going to close yep there we go app three has stopped now why did this thing crash this is like the programmers hell my thing just crashed i don't know why um so what we're going to do is we're going to actually get a little quick mini tutorial in debugging you see this little debug icon right here i'm going to click that click our emulator bring our emulator up and you notice that i says waiting for the bugger you'll actually see this physically on your phone now the debugger is attached so if we hit result and we close this boom now we have our stack trace fatal exemption main java lang runtime failure delivering result info you'll see that quite a bit in your errors but why why what happened null pointer exemption attempt to evoke virtual method blah blah blah blah blah on a null object and then it gives us the line and everything on where it happened you just kind of gotta follow it through it's why it's called a stack trace what's happening here is our intent is null so we're going to say if all right so we're going to say data is null we're going to actually turn that into an error for our logging so we can see that beautiful little thing so let's run this again get our emulator up and going and i want to see that monitor and result clicked we're going to close this now notice how we've got our debug messages we have activity result the request code is one the result code is negative one data is null notice how that lines read it's because we did that as an e or an error request code that tells you what we did feed customer is one the request code is one we can say 99 1 billion 1062 whatever we want point is if you're going to have say five different buttons and all of them you want to start an activity and get a result that is how you would determine you would just simply do a switch statement saying you know i want to know what the request code was the result code is well you guessed it the result here the set result so we're going to say if result now i cannot spell result code equal result okay then and quest code equal oops what did we call that pesky little guy feed customer that's right so if we had a result of okay and the request was feed customer then let's do something here and let's do this actually let's not do that just yet let's just say we fed the customer customer is happy else customer is not happy and we're just going to say nope all right so some basic java here what we're really doing is we're saying all right launch this thing for an activity i'm sorry launch the activity for a result that's how hungry i am and then when it closes we're going to get the activity result now they're either going to back out of it which is a cancel or they're going to click close which is going to save your data do whatever you want to do at which case we have successfully fed the customer otherwise nope so let's run this again and bring up our monitor here let's close that out so we're going to go result and we're going to click close just to see what it does and it says the data is null we fed the customer now why is it still saying data is null because we haven't returned an intent from the customer class which we'll do here in a second um so we did feed the customer meaning we know that we opened the customer activity and we got a result okay back because they actually clicked the close button so what we want to do in the customer class here is we want to say hey there was an intent i know there was an intent so we're going to get that intent and we're going to say if whoops intent dot has extra and we're going to give it a name of what it was it food we're going to say long dot d app settings dot tag comma i'll say thank you whoops see i love pizza well i love and then we're going to actually get the value here intent get extra now you notice how on the get extra it's not get extra it's get string extra or uh get boolean or there's all tons of them but uh you can get get it back as a boolean as an integer as whatever really you want um if it doesn't have that we're going to say log e app settings dot tag big bold letters the customer's not happy where is my food because i'm starving all right so let's look at the program structure here we've got two different buttons we have start and result start is just starting the intent it doesn't or starting the activity it just does not care just here start it result however we've got intent and we're going to put an extra called food we're giving them pizza and we're going to start for result meaning we want to know what happened this is the like the waiter walking up going how is everything you know making sure you're not mad and then when we get the result we're going to kind of log some things out here and then we're going to say hey is this intent null yes or no um is the result code okay and actually let's flip these around so it's a little easier to read we're going to say did the request that we make equal feed customer meaning is that what we were doing up here and then the result code is it okay if yes we fed the customer otherwise nope we're in trouble so let's bring up our monitor clear that up stopped it run this all right let's see what do we want to do first we want to go result and we want to make the customer mad we're just going to go nope customer is going to go nope and if we start this notice how nothing is just on started because we're just starting and we don't care so we're going to do result again let's close clear all this out now we want to actually close this we fed the customer so we know that feed customer was called and the result was okay meaning they actually click that close button they said thank you um and it even says thank you I love pizza because we were sending that now that's via the intent now we want to actually get some sort of result back here so we're going to take this set result move it down here and we're going to say intent set extra I'm sorry put extra let's call this comment thank you so that's the customer's comment and we're going to take this intent shove it right back into set result let's go at into here and if our data is not no we're going to say has comment has extra comment let's not do that let's do this let's see if they actually had a comment my brain is like hitting you know critical mass here because I have not eaten and I'm like really starving so this is how we're going to pass data back here I want you to really understand this we're going to do the set result we're going to say okay but this time we're taking the intent so we're taking the intent that we're just getting this is the same intent that was used to call and we're going to push it right back through the set result so if we look at this graphic here the intense created pass the system blah blah blah activity b is getting that same intent we're just going to pass it right back to activity a after we've modified it a little bit of impromptu I've never actually tested this because it's bad design and I'll explain why here in a second I just want to demonstrate that it is doable so we're going to hit result and we're going to hit close and the data is thank you meaning customer in the customer class is thank you I love pizza but then in our customer class we're putting an extra called comment thank you and we're printing that out right here by the data get string extra I hope that's not too confusing data is an intent so we're getting the string extra out of the intent remember an intent is something you intend to do with that intent you can put little extras in there and that's all we're doing is just grabbing those extras now that is horrible design do not do that and here's why when you look at your manifest you notice how these are completely different these two activities right here totally different with different filters different categories etc etc part of the android design is that you can call an activity from another program that's how dialer works so if you're on like facebook you want to call somebody hit the call button it brings up your phone dialer facebook didn't recreate a dialer from scratch it's actually calling the dialer activity from the android system so you can make parts of your activities or parts of your program these activities open to the world so you could say hey you know this customer activities really cool why don't you guys use it in your programs and they can call that directly completely bypassing your main activity so if we go back to our graphic here they can call activity b and activity a never has a clue that even happened not a clue um so part of what needs to happen here if you go back here you see right here this little comment to do save save save save the reason why you do that is because if you're calling that that data is lost forever because it's not being returned now the calling application can do an on activity result and they can read the intent and things of that nature and that actually happens quite a bit but you really shouldn't do that um this kind of a no no um the reason why it's a no no is because you can lose information you can forget to save it it's just bad design by nature i know i'm going to get some hate mail saying that's the way it's designed it's the way it's intended that's why you have this intent data but i'm telling you right now for a professional grade application do not do that actually persist onto a file um something of that nature a file a database which we'll cover in future tutorials that way you know even if you're within your program and it crashes you've still got that data you didn't just lose it forever um now that being said in future tutorials i will absolutely use this intent data even though i've just told you not to do it because with everything programming there's more than one ways to do it so to recap what we've talked about is how to take an activity create an intent launch another activity and even how to get a result back from that activity so you've kind of seen the full life cycle here um whoo that was a lot of talking on my part i hope you found this educational and entertaining and uh be sure to visit my website void realms.com the reason being glad you stuck around is we have a void realms facebook group so if you go to contact you can click on that we have a thousand programmers out there that are really helpful i use it as a resource every single day um some of the folks in there make me feel really stupid because they're much smarter than me um and i'll load the source code into the android why is that at the bottom i thought i alphabetize this anyways i'll load the source code for this and all the other tutorials one final note this site is 100 funded by your donations if you're a college student you can't afford it do not donate but if you're like a big fortune 500 company and i just saved you 10 million dollars sending your people to training then by all means you know send me 10 bucks so i can help keep the website running