Activities and Intents (Android Development Fundamentals, Unit 1: Lesson 2.1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
♪ (upbeat electronic music) ♪ Hello, everyone. I'm Lalit Singh. So now, we have looked at how exactly we can create a very basic app, how exactly we can include views, how exactly we can handle our views. Next, we're going to take a look at activities and intents. We have already been through what exactly activity is. The whole screen which we see on your Android device is known as activity. So now, we are going to go even deeper to understand what exactly or how exactly the mechanism of activity works inside the Android. So in this session, we're going to cover about activity, how we define the activities, how intents are helpful in working the activities, how exactly we can transfer data between two activities. So all this is going to be covered in the session. So first of all, what exactly is an <i>Activity</i>? Activity is an application component which we discussed. It's one of the application components. Last we discussed, there were four: activity, service, broadcast receiver and content provider. So Activity is an application component. And whatever you see on your screen, all the UI which we saw in our last applications, they all were presented by the mechanism-- They all were presented by a class <i>Activity</i>. So <i>Activity</i> comprises of two things, a Java class and an UI. That is a layout, right? So, <i>Activity</i> can be shown as a part of application screen itself or it can be a floating activity. That is it is in the background. If I have to give you an example: whenever you drag your notification bar, you see your activity is still in the background. So, again, that notification bar is now what? It's floating over your another activity. So <i>Activity</i> is what? <i>Activity</i> is whatever you see on your screen. It can be a complete window. It can be a floating window-- basically, under the hood it's a class with an UI. So what exactly can <i>Activity</i> do? Well, if you are designing an application for e-commerce, then you can design an activity to place the orders. If you are designing an application of social app, chatting app, then you can design an activity to create a chatroom, right? So, all the other UI components which we just saw in our previous session, buttons, TextView, ScrollView, they all are being presented to the screen with the help of <i>Activity</i> only. Until there is no activity you can't even see all those UI components. So activity under the hood is responsible for displaying your whole UI. So, <i>Activity</i> has a lifecycle that starts with <i>onCreate</i>. It goes to start application, and it runs and it can be paused. It can be stopped also, and it can be destroyed also, or it can be restarted. So we're going to go through the lifecycle in our next session, but for this session, let's just talk about more in activities. So, if I have to show you examples of activities I think you already know. Well, anything you see on your screen. You have already tried examples of that. All the applications you have created up until now, well, you were able to see where exactly they were shown. They were in <i>Activities</i>. And you can see some inbuilt examples like you open up a Calsie, and the Calsie is opening up. What exactly is that? Those buttons and those added texts, they are shown inside the activity. You open up a map application, that's also an activity. An application can contain one or more than one activities. Generally, when you create an application, it will be tons of activity. It will be the combination of all the activities together, and then you are navigating through one activity to another activity. But there will always be one main activity. What is that <i>"main activity"</i>? We call it launcher activity also. You take any application, any application out there, whether it is a social app, whether it is an e-commerce app, whether it is a company app. Whenever you open up that app, you always see a particular screen at the very start. >From there, from that particular screen, from that main screen, you can navigate to other screens, but that particular screen will always come whenever you open that app. So that screen is known as <i>"main activity"</i>. You can organize your activities in the form of parent and child also. Like from your <i>main activity</i>, you get transferred to some other screen, and from there if you want to go back, you can always go back to your parent activities also. So we'll see how exactly we organize our <i>parent-child relationship</i> also. Of course, it has to be done in Android manifest file. Like we've discussed in our previous session, that Android manifest file is a file which will give you the information about the application itself. So, if you are creating more than one screen in your application, your Android or your application knows about it. So that's where you need to put all the activities. You need to register all the activities. <i>Activity</i> is not just a Java file. It also needs an UI file. If you want to show any UI over the screen, you need to create the UI first in your XML file and then connect that XML file to your Java file. That's when the activity becomes whole. Your Java file plus your XML file makes your activity complete. And whatever you have designed in that XML file as UI, it appears on the screen. So let's just discuss about how we implement activities now. We definitely need an UI first, so we will go and create an UI in our layout folder, which is inside our res folder. Of course, we need a Java file, which we have to extend with a class <i>Activity</i> or <i>AppCompatActivity</i>. Now <i>AppCompatActivity</i> is also extending from your <i>Activity</i> class only. So, we can always use <i> AppCompatActivity</i> also. Then you need a way to connect your XML file with your Java file, and that's when <i>onCreate</i> comes in. This is one of the lifecycle methods of your activity. And you want to connect your UI bar to your Java file at the very starting of your activity itself because <i>onCreate</i> is the lifecycle method which runs at the very beginning of the activity. So at the starting only, you want to give the UI part to your activity. And after that, even if you do all these things, but you forget to declare that in your Android manifest file, it's no good. You have to put that in your Android manifest file also. So you have to declare it in your manifest file. If you follow all these steps, then only your activity is perfectly registered, and you will be able to see your activity on your screen. So, let's just talk about XML file, how we define your activity in an XML file. Well, XML file is nothing but a layout file. So it will have a root layout, a <i>ViewGroup</i>, which we have been discussing about until now. So <i>RelativeLayout</i> or <i>LinearLayout</i>, it can be any layout, and inside that as many as we want. As of right now, we are putting just the <i>TextView</i> and that text says, "let's shop for food." And this layout I want to connect to my Java file, so for that, I need to create a Java file for the activity also. That's what my Java file looks like of the activity. It can be any name, <i> public class MainActivity</i> or it can be public class ABC Activity extends <i>AppCompatActivity</i>. Now, <i>AppCompatActivity</i> is a class which is again extending from the activity only. So you could have given<i> extends Activity</i> also. So why exactly am I given <i> AppCompatActivity</i>, why not <i>Activity</i>? The thing is if you are designing an application, which will work only for the latest version. You do not bother about your previous version of Android to run your application. At that point in time, you can perfectly extend it from your-- You're allowed to extend it from your activity because you are not bothered about the previous version. Because in your latest version of Android, there will be features which will be supported like material design or like new security runtime permission. But those features were not introduced in your previous versions, like 4.0, 4.4. So if you want those features also to be supported in your previous versions, that's when you use <i>AppCompatActivity</i>. Because we are not only targeting the latest versions of Android because if we go about doing that we won't be able to connect to the majority of people, and we want our application to be used by a majority of people. So that's the reason we are using <i> AppCompatActivity</i>, and this class has been inherited, or this class we are using from a support library <i>AppCompat</i> which we've discussed on our very first slide. Next <i>onCreate</i>. <i>OnCreate</i> like I told you is one of the lifecycle methods. This is the very first lifecycle methods in your activity which is being called whenever your activity is shown to the user. So that's where you want to initialize everything for your activity, right? So before actually doing your own customized initialization, you do want to initialize whatever your parent <i>onCreate</i> was doing because this is the concept of inheritance. I'm overriding it, so that's why you want to call <i>super.onCreate</i> so that you can get all the features from your parent class, of course, plus now you can go ahead and customize it by attaching it with your own UI also. How do you do that? Well, that's how you do it:<i> setContentView</i>. <i>SetContentView</i> is the method which will help you attach your XML layout which you have already created. So, for that we already know that that is an R or a Java file which takes care of all the resources which you put in your res folder. So how do you access it? <i>R.layout.activity_main</i> and that's the name of your XML file. And now they both have been attached. Now am I good to view this activity now? Have I completed all the steps? No, we are still remaining with one step that is registering it in my Android manifest file. That's how you register any activity in your Android manifest file with a tag <i>activity</i> and the attribute <i>android:name</i> and give the name of the activity. Now if you want to provide a behavior to a particular activity, let's say there might be intent Activity inside your application. Now which activity do you want to make launcher activity? Which activity do you want your user to see every time he opens up your application? Well, that's when your <i>intent filter</i> comes. Your <i>intent filter</i> provides a specific behavior to a particular activity. Now you will see that <i>intent filter</i> not just provides the behavior to activity only, but it can provide the behaviors to other Android components also, like services and broadcast. That we will see in our further sessions. For now what exactly is this <i>intent filter</i> is doing here? It is telling that this activity will be a <i>main activity</i> that is by providing an action as main and also telling the Android system that this will be a launcher activity for this particular application. So there can be only one launcher activity in an application. So every time I open up my application, this will be the activity that will be shown to the user. If you have a Java file for this, and if you have a correct XML file attached for this, every time you open up, this will be the activity that will be shown to the user. So that was all about activities. Now let us go even more deeper and understand about the concept of intents. Because under the hood all the activities you see, all the screens you see appearing up on your Android device, they are all being launched by your intents only. So let's just talk about intent first. Intent is a class which is used to invoke all the Android fundamental components except content provider. So you will invoke activities with intent. You are allowed to invoke services with intent. You are allowed to invoke your broadcast receiver also with intent, but you don't want to invoke your content provider with intent because content provider specifically deals with data, and you don't want anybody to hack or tamper with your intent and lose your data. If I have to tell you about how exactly any application launches. You see you click on your home button and there are so many applications. You click any one of those applications, and one of your screens opens up. What is exactly is happening there? Whenever you click on any app, there is an intent which has been fired, and the Android system knows that, "now, I have to open an intent." Who is what? Who is a main and a launcher? Because for that application we must have already defined a main activity which is a launcher, right? So every time you open up an application, there is an intent fired and Android system says that, "okay, this is the particular intent that I need to open up," and it shows that particular activity. To make things more clear, let us talk about intent more, what we can do with intents. We can start activities with intents. Well, how exactly you can start? You can create a button, and then click on a button, and start a new activity with that. You can create a bunch of buttons, and you can go to different activities. You can navigate to different screens from that. You can use it for that purpose. Or if you want to share a particular data; at that point in time also, you can use intents. Let's say you want to share your photograph with another application. That's when also you can use intents in your activities. Well, you can start your services also with intent. Let's say you want to initiate a download or you want to open up a music. So how would you-- We all know that we open the music and the downloading part goes into the services, but how exactly do services get involved? That's when your intent comes in. Your intent has an important role to play to start your service or to start your activities, and the broadcast receiver also. When we talk about intents, there are specifically two types of intents. What are those? First is explicit intent, the other one is implicit intent. When we talk about explicit intent-- in explicit intent, we know the source and destination. What do we mean by that? We exactly know from where exactly we are transferring and where to transfer. And generally you will be using this kind of intent inside your application itself. Let's say you have 10 activities. Now you are at a particular activity at that point in time, and you click a button. You want to open up the seventh activity, so you do what? >From that particular activity, you want to transfer to the seventh activity. So you know the source; you know the destination. So you specifically know what you want to do here. So when we talk about implicit intent, implicit intents are used to invoke the activities from different applications. For example, let's say you make a call, and you have a dual SIM phone. Now, at that point in time, let's say you have two SIMs installed in your device. Now, you must have seen it asks you two options, which SIM you want to call. So what exactly happened here? You just provided action. You want to make a call. So your Android system started searching out for all the applications who can handle calls, and it found out that there are actually two applications which can do that because there are two SIMs installed. Now let's say there is only one SIM inside your phone. At that point in time, it won't even ask you from which SIM you want to make a call. So this process of searching out within the whole Android system is also known as intent resolution. So how exactly are activities started then? Whenever you are using explicit intent. First of all, to start a different activity, using an explicit intent, you have to create an object of intent. That we can do by calling the intent class available and inside that, this is what we call source and destination. Source is this, this means current class, our current class object. The second one is known as the name of the activity. We will know the name of the activity inside our own application itself. If we are creating our application, we will know all the names of the activities which we are dealing with. So we can always use explicit intent to transfer between activities within our own application. But this will only initialize the intent. To trigger the intent, you need to always use <i>startActivity</i>. Until you trigger your intent, you will not get transferred to your second activity. So <i>startActivity</i> and provide the object of intent inside that, and you will get transferred to the next screen. How do you use implicit intent then? Well, in case of implicit intent, you do not know the source and destination but what you do know is which action you want to perform and what is the URI or the data which you want to pass when that particular action is performed. For example, if you want to open up a webpage, you would like to transfer a link that is <i>http://www.google.com</i>. But if you want to make a call, that is a call action, you would like to transfer a phone number. If you'd like to open up a map, you would like to transfer longitude and latitude. That's how you can open different applications and different activities of different applications. And, again, to trigger that, you have to use <i>startActivity</i>. So let us take a look at the examples of implicit intent now. So now let's say you want to open up <i>google.com</i>. So first of all, you will provide a URI and change it to the URI data, and then you will initialize your intent. While you are initializing your intent, you need an action and the data. Data you already know URI. Action you are putting <i>ACTION_VIEW</i>. And <i>ACTION_VIEW</i> is a generalized action. There are so many actions. If you want to make a call, there will be <i>ACTION_CALL</i> also. If you want to make a dial action, there will be <i>ACTION_DIAL</i> also. If you want to send SMS, there will be <i>ACTION_SEND</i> also. And all these actions are very hard to remember. Now if you want to make a generalized action so that you don't have to remember each and every action, you can always use <i>Action View</i>. And it will, depending upon the type of data you are passing, it will perform according to the data that you're passing. Let's say if you are passing an HTTP request, then it will always open up your browser. Now, let's say if you are passing a call or if you are passing a number, at that point in time, the URI will change from <i>http</i> to <i>tel</i>. Now, this particular thing which you see here, that is <i>http</i> or <i>tel</i>, this is known as data scheme. Now, according to this data scheme only, your <i>Action View</i> will start performing. If it sees that the data scheme is <i>tel</i>, it will start looking for all the call applications. If it sees that your data scheme is <i>http</i>, it will look for browser. If it sees that your data scheme is <i>geo</i>, it will start looking for all the map applications. And at the end, you have to trigger your intent by using <i>startActivity</i>, and it will open up the browser. If there are more than one browser, it will ask the options from you, in which browser you want to open it up. That's called intent resolution. That's the process called intent resolution because it searched out in the whole Android. But if there is only one browser, it will never ask you. It will open up straightaway in that browser. Now let's just take one more example where you do know the specific action type. Well, again you will provide the URI. Here, the data scheme you can see has been changed. It's <i>tel</i> and you provide the number. And then, again, you initialize the intent, and this time you know what exactly the specific action is. So <i>ACTION_DIAL</i> and provide the data. And, again, you will put <i>startActivity</i> and it will trigger the intent, and you will be able to open up a dial application. So you see it's totally dependent upon how exactly you want to handle your implicit intent. If you know a particular action, go for that particular action. If you don't know, then use the generalization. So, now we know how exactly intent works. We know what exactly activities are. So let us see how exactly the whole application, which we see, which we navigate-- we click on a button and the application opens up-- how exactly that thing happens. Whenever you open up any application it may contain 10 activities inside it. Now, out of those 10 activities, there will always be one launcher activity. Now, whenever you open up application, that launcher activity does have the behavior defining your intent filter. So that intent gets fired, and Android system says, "okay, this is my launcher activity" and it opens up your Main Activity at the very beginning. Now, from there, let's say you have a button to transfer you to a shop activity or a different activity. You click on that button. You will again use which type of intent this time? Inside this, you will use explicit intent because you know that that activity actually resides inside my application only. So again, Android system will fire that intent and open up that second activity. >From there, you can navigate to third, fourth... So in this way, you can always use intent to navigate between different screens. So now you have seen how exactly you can transfer between screens, and now you are no longer just working with a single screen which up until now we have been working with. Now, it's more fun to work with your application. Now it's good. You can transfer between these screens, but what happens when you want to transfer the data also? So that's what we are going to see how exactly we can use our intent itself to transfer the data in between applications. So, when it comes to transferring the data between activities, intents can be used in two ways. First, using the <i>data</i>. Second, using the <i>extras</i>. The data part, we have already seen when we did the implicit intent. We provide a particular <i>URI</i> and accordingly with the help of intent resolution, it provides the same data to the next application. It can be any location, it can be any phone number, it can be any <i>URI</i> link also. What about <i>extras</i>? Well extras transfer the data in the form of key and value pairs and that data is being saved in a class known as <i>Bundle</i>. Let me go through that <i>Bundle</i> again. <i>Bundle</i> is a class which is used in the Android to transfer the data or to save the data dynamically. What do I mean by dynamically? As soon as you close your application, as soon as your application is out of the memory, your data also goes away. So you can transfer the data while your application is still on. And that's all I want. I want to transfer the data between two applications. I want to transfer the data between two activities. So, for example, if I have to take an example, let's say I have first screen, and in that screen I put a name. Let's say, Lalit Singh. And when I click on that button-- When I click on a button there is a button which transfers you to the next activity. It takes you to the next activity, and whatever I put in that previous activity, that name, that name should be transferred to my second activity. So that's when you will be using extras with the help of intents. So how do we use them? Well, you create an intent object first of all like we have been doing up until now, explicit intent object, you know the source, and you know where to transfer. Second, this time, instead of directly calling <i>startActivity</i>, before that, you will put the data inside the intent with the help of extras. So that's one way to transfer the data between activities. This is the way when you want to transfer data from the first activity to the second activity. Now, what if your first activity is expecting the data from your second activity. For example, let's say you are making a payment on eBay. At that point in time, you will provide your card it is on your first activity. And you will click pay button, and it will go to the second activity. And now you are expecting from second activity to give you the result back whether the transaction has been completed or failed. So that's when you can also transfer the data from the second activity to the first activity. So we will see an example of that also, how exactly we implement that. Let us just learn how exactly we can transfer the data from data first, the first spot which we have already seen, like if I want to transfer to a particular link Google page or if I want to transfer a telephone number, that's how you do it,<i> intent.setData</i>. And inside that, you can put whatever data you want. Like here I want to put a link. Or if you want to transfer to a particular file inside your <i>sdcard</i>. So that's where you will put again <i>intent.setData</i>, but this time the <i>URI</i> will change. Instead of just passing directly a link, it will become a file because you want to transfer to a file. What if you want to transfer the data using extras? Well, how do you use the extras? There is a method named <i>putExtras</i>, and inside that method you have two arguments. First argument is the name of the key. Second argument, the value you want to transfer. If it is an integer value, you will use the second argument as an <i>int</i>. If it is a string value you are transferring, you will use second argument as <i>string</i>. If it is an <i>array</i> you want to transfer, then you will use the second argument as <i>array</i>. But the first argument will remain <i>string</i> only because [as it] is the name of your key. So, you can see that in example that, let's say, I want to transfer an <i>array</i> then I created a food list, and inside that food list, we have rice, beans, and fruit. Now I want to transfer this whole <i>array</i> to my next activity, so that's when I will put this inside my <i>intent</i> only. I will use the intent object <i>.putExtra</i> and use a key. It can be any key. It can be <i>Key 1, Key 2</i> and then transfer that <i>array</i> here. So let's just-- and then you trigger. This time you can see the key. The key can be anything. You can do hard coding also or you can declare as a constant at the top also. So whenever you are accessing your data in my second activity, you will always need an object of intent first. So if you have the object of intent, and you are accessing a specific data type then you will use the object of intent, <i>.get</i>, the type of data and extras. Like if you are using intent it will be <i>getIntExtras</i> and the key, the value which you save there or which you pass at that point in time. If you have given <i>Key 1</i> there, it will be <i>Key 1</i>. If you remember in our previous slide, we pass <i>putExtra</i>, there was a key and then the message. So this key is the one you will be using to access the data in my second application. So you'll see that the first argument of <i>getExtra</i> is always the key. If is 11, I must have defined 11. But if you want to get all the data together, at that point in time, you need to do <i>intent.getExtras</i>, and it will give you the object of a class <i>bundle</i>, and from there you can access each object independently. So what happens when your second activity is giving results back to your first activity? So at that point in time, you will be always triggering your activities by calling <i>startActivity</i> for a result. And in that case, when it reaches to the second activity, in that second activity, you have to provide whether the result was okay or result canceled. Based on that, whether the result was okay or canceled, you will perform operations, again, back to your first activity. So this is how you transfer to your second activity whenever you are expecting result back from your second activity. You don't use <i>startActivity</i>. You will use <i>startActivity</i> for a result. You will pass that intent object where you have specified in which activity you want to transfer. Plus there will be a <i>requestCode</i> also. Why a <i>requestCode</i>? Because there might be a chance that you are getting results back from different activities. So, how would you distinguish if you are getting results back from a different activity? So that's when your <i>requestCode</i> comes in. <i>RequestCode</i> is nothing but an integer value. So we can give an integer value there. So when your second activity is done, how do we get the result back in my first activity? So that's when a <i>callback</i> method is called on activity result, and this is where we will handle it. So this is how we trigger our first activity. I will tell in which activity I want to transfer. That is an explicit intent, and this time, not <i>startActivity</i>, but <i>startActivityForResult</i>. And pass the intent object and a <i>requestCode</i>. It can be any integer value. So now in your second activity, when you want to transfer the data back to your first activity, this is where you will put a normal intent object. And in that intent object, you will put your extras. It will transfer your data in the form of key and value pair. And then you will set the <i>Result_OK</i> and transfer back your intent to your first activity. While you are doing that, you have to finish your second activity also so that you can get back to your first activity, and in your first activity now, automatically, the <i>callback</i> method<i> onActivityResult</i> will be called. Now this is where you would check whether this is the same <i>requestCode</i> data which I'm getting from my second activity or not and whether the <i>resultCode</i> from there is okay or not. If it is okay, access the data. And if it is not okay, you can always put an as condition and handle it a different way. So, now, let us learn how we can navigate through different screens with the help of intents. First of all, we need to learn that every time we are transferring to a different screen, the first screen is not getting destroyed, it's always getting into the stack. So if you are on your main screen, you click on a button and you are transferring to the second screen. Now the top of the stack is your second screen, but your first screen is still on the stack. So whenever you click your <i>Back button</i> of Android, your second screen gets destroyed and, again, the top of the stack becomes your first screen. That's the reason you can always see your previous screen or previous activity on your screen. So, if I have to talk about it, let's just say you start from the <i>MainActivity</i>, you click on a button, you get transferred to the next activity. >From there also you click on a button, you get transferred to the third activity. >From there you click on the back button, so what happens? Whenever you click on the back button, you'll get transferred back to your second activity. You will never get transferred to your first activity because the top of the stack right now will be your second activity. Now from there, again, if you click on next button, you will again get transferred to the third one. Now from there, again, you click all the way back to the first activity, you will get transferred to your <i>MainActivity</i> unless from <i>MainActivity</i> also you click the back button, then your application gets destroyed. So there are two ways you can navigate to your screens. First one, by clicking your back button. In that case, you always put the stacks-- your screens are always kept in the stacks. The second one is your Up button which is generally included in your action bar which gives you a property of <i>parent-child relationship</i>. So, now, let's just see what exactly back navigation does. Like I told you before, back navigation will always look at to your stack. So, if there are seven activities opened up and you are on your seventh activity, when you click on back button, you will always get transferred back to your sixth activity. Now, you must have seen you can always switch between the activities with the help of back button also. Let's say you are on your current activity and when you click on a link, you get transferred to a browser, and after you are done with browser, you again click on back, you will again get transferred back to your own application. Because that's what's in the next top of the stack in the back. When we talk about up navigation, it provides you a parent-child concept. You don't have to really get to the back of the stack-- You don't have to really get to the stack here. You can define the parent activity, whichever parent activity you want. Let's say you have opened the seventh activity. Now, from seventh activity, you want to navigate back to the very first activity. So all you have to do is, in your manifest file where your seventh activity has been declared, you will provide an attribute known as <i>android: parentActivity</i> whichever the activity you want to make it parent. So when you specify that, now when you click on Up button, you will always get transferred back to your <i>Main Activity</i>. You will never get transferred to the top of the stack. To learn more about your activities, intents, transfer data between activities and the navigations and all, you can follow the following links. Next, we are going to see the practical part of how exactly we can implement the activities and intent together. ♪ (upbeat electronic music) ♪
Info
Channel: Google Developers India
Views: 44,969
Rating: undefined out of 5
Keywords: Android, activities, activity with an intent, data, product: android, fullname: other, Team: Scalable Advocacy, Type: DevByte, india, tutorials, GDS: Yes;
Id: 3dsAuLkDTUc
Channel Id: undefined
Length: 36min 26sec (2186 seconds)
Published: Sat Feb 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.