 Hello everyone quick accompanying video to explain this week's topic which is multiple activities and intense Intents are the mechanism that Android uses to start different screens or different activities in your app So if you haven't finished chapter 2 lab from headfirst Android development, please do that before you move on here and before we get into this stuff of with multiple activities intense and Intense I want to raise something that Some question that some of you may have had from chapter 2 I so in chapter 2 labs where you were just learning how to build interactive apps the headfirst authors had you create the beer expert and A question I get quite a bit is what is the point of having this beer expert class that they have you make? Why not just have everything you need and find beer activity dot Java right seems like the beer expert is just Out there. It's just extra work Yes, you could have everything in find beer activity. You you could do that. Maybe you did do it even though they asked you not to but having this the code in the beer expert that you put there with the different flavor types and That's having to do with the colors What that's good design and but the question is why it seems like extra work? Well, you are going to find out in this chapter, but let's give you the answer. Okay, so Android Recommends that you follow a design pattern now design patterns have been around in software engineering Probably since about the mid to late 80s early 90s when object orientation really really took off so did design patterns, but and and I Kind of misspeak here because the model view controller Design pattern that we briefly touched on last time has been around since GUIs were first realized on the Xerox Alto But design patterns are just a fancy way of organizing your code or code modules or Objects around one another in a sensible way and there are very specific design patterns that you can use to solve Problems that come up over and over and over again in software engineering And one of those is the model view view model pattern and it's called that because of the its constituent parts Okay, so the view Just like in model view controller is the Part of your code that shows something to the user. It is the display. So in Android apps. This is the layout in The second component is the view Model the view model in Android is your activity class Okay, so you have the view which is the layout XML file that sets the stage and then you have a view model And between the two is some sort of data binding, right? So data binding like the text fields in your app The data is shared tightly between these two button presses the the data that is Transmitted between when a button is clicked is tight between these two and they can go in either direction Right. The other thing here is that views and view models are inseparable. They are tightly coupled They do not exist without one another in this case Okay, so you've got a view that displays things and you've got a view model that is responsible for updating the view And then the final thing is the model and that's what the beer expert is Okay, the model is data It's business logic. It's stuff. That's not directly attached to the user interface Okay, so this is model view view model and it's a pattern and it's not particular to Android You can find stuff that's very similar and iOS development in native Windows development native Mac OS development It's just a term right but the idea is you got the presentation layer the layer that kind of controls and interacts with that responds to user Input and then all the other business logic that can kind of live separately So in the beer advisor right the view is your fine beer activity dot XML And the view model is your fine beer activity dot Java right these control the user interaction the model is The beer expert Okay, so if you look at the beer expert right like the view model kind of depends on it But it does not depend on the view model in any way. It is not tightly coupled to the view model It's there more of a reference the view model uses it and it's got some good information So but why split this good information out from the view model? Well, the answer will become apparent in this chapter, but the reason is You can share this business logic with different Views and view models or different activities in the case of Android right so in chapter 3 You're going to create an app with multiple activities in it But you want to share between all those activities the concept of you know beer colors and descriptions and names Right well isn't it so much better to just maintain that list of beer colors and names and things like that in one place? Which is the model? Instead of keeping a list here and here and making sure that you keep them Updated you know your list of colors is the same in here as it is in here, right? Why not just put them all suck them out here so that they're shared, right? That's the idea so this is kind of Falls under the general heading of separation of concerns, which is very prevalent in computer science You probably heard this term in your 331 your object oriented programming class in conjunction with information and encapsulation But in general software systems should strive to separate the Presentation logic the presentation layer whatever that user interface is and it's not not just in Android But in say like web development or if you're making ATM machines or point-of-sale systems separate the presentation layer and dealing with displaying and interacting with the user from Business logic right the sort of general processing general data handling that all computing systems have All right, so going back to the issue of having multiple activities and intents most applications have Multiple activities they have multiple screens here screenshot from the canvas app that we use at UNCW Different screens each one of these screens is a different activity So but the question that we have now is well, okay how do I create different activities multiple activities and how do I tie them together because in Your code that you've seen so far you have never seen this right you have never seen a call to new and That kind of like opens up the new activity you just defined that just does not happen Okay, so how do you get a new activity kind of know how to define them already? That's that's not the hard part, but how do you launch a different activity? Well, the answer is in the concept called an intent the Android class and intents are Android classes for starting activities and Sharing data between activities Okay, so for example if you're in the canvas app and you tap on this button here Which is for CSE 315 mobile application development? And you want it to start a new screen that perhaps shows you some details like this one You're here, and you want to get here. How do you do that? First you create an intent. It's a built-in class You Create a new one the and its constructor takes two parameters This which is referring to the calling context the calling activity Because when an activity is started it needs to know who who started me was was it some other activity? What started me and then course menu activity dot class So this is an activity class. It is a dot class Type here, and this is what we call an explicit intent, which sounds like the name of an awesome like Bruce Willis movie, right? Or Steven Seagal This what makes this as explicit is that you the programmer are saying when you press this button I want a very specific activity to start next. I want a new screen to come up you next and that's course menu activity Okay, you can also Send data to that new activity so intent dot put extra a String course ID and then a value. This is some variable value Think of this as a dictionary. Okay, or a map the intent has a built-in map or a dictionary called an extra It's called the extras. Okay, so this is a key a Key as in a key value pair from a dictionary a key You can name it whatever you want and then the value that goes along and what happens is it gets packaged up in this intent? object and it kind of goes through the nether Because you will then call start activity with this intent start activity is a baked in inherited function for our inherited method Excuse me in the activity class and what it does and this is a really important point for you to understand It basically asks Android the operating system Hey, can you go look for this activity that was specified up here and start it up for me? Kind of like when the user on the home screen of their app pushed a button to launch your app Then pushing that button kind of said hey Go find the explicit that go find this activity Associated with this button and start it up and you got your home screen, right? Same thing here. You are asking Android. Hey, go find this guy and start it for me, please. Okay, and Android will try and comply with you. Okay, but this is very different from like Instantiating the the new activity directly. There's no calls to new here All right, so just a word about design here It's pause here for a second You know what we'll skip that we'll come back to that. Sorry don't want to confuse you We'll come back to that at a later time. All right. That does not look good. Let me Crunch this up. There you go So another kind of intent is what is called an action. Okay, and you you have used actions on your phone Actions are when you let the user choose which activity handles something, right? So this is cool. Your app doesn't have to do everything Sometimes it's nice to give the user a choice in which app they use and then maybe you can use the result So for example sharing right everybody's got the sharing button in their app And when you click sharing you get a list of things a List of different apps that can handle the concept of sharing like a text messaging app an email app might come up Snapchat Instagram might come up if you click this icon So what you're doing with an action? This is a term you need to know as you're saying hey user Please pick the one of these things you want to start up to handle this You may also like start up a camera so you don't have to code your own camera start up a calendar app Etc etc Okay, so You also use Intents to realize these activities, okay So say you've got a picture here, and you want to share it so you click on the share button Okay, in your code the user has clicked the share button. What are you going to do? You are going to instantiate a new intent, but instead of it being explicit and having a target class It's going to start You are going to use this constant intent dot action send Okay, this is there's a whole bunch of these action send action camera action call action SMS short message service and These things are called implicit Intents the other ones were explicit because you're explicitly defining which class you want to start these are implicit You say hey, I kind of want to send this picture to somebody else go ask and Here in the case of excuse me in the case of images in our intent we can set some information about it This is a JPEG image Okay, and then we make a call Let's go inside out first intent dot create chooser with our intent our share it's a variable with an intent and Just a label So what happens here intent dot create chooser you can see this is a static call Android looks at the type of thing you want to share and it sees oh this person wants to send it What type it is and then it pulls up a list of other apps who are registered with the Android operating system to handle the sharing of JPEG images Okay So then the user picks one of these things It comes back so this whole intent dot create chooser has evaluated and it's got a class in it And then you start that activity. Okay pretty cool Pretty cool stuff All right, so you will learn all about that in chapter 3 in the lab for chapter 3 Before you get into that make sure that you have set up the lab on setting up get and get hub for your homework for your project and setting up Things for how to commit those projects on your computer work on the lab for chapter 3 and once you finish chapter 3 you can do 90% of assignment number one so get into it. Let me know what questions you have and good luck