What you’ll achieve:
In this project-centered course*, you’ll design, build, and distribute your own unique application for the Android mobile platform. We’ll provide you with a set of customizable building blocks that you can assemble to create many different types of apps, and that will help you become familiar with many important specificities of Android development. When you complete the project, in addition to having a personalized app that you can use and share, you’ll have the skills and background you need to move on to more advanced coursework in Android development.
What you’ll need to get started:
This project-centered course is designed for learners who have some prior experience programming in Java, such as an introductory college course or Coursera’s Java Programming Specialization (https://www.coursera.org/specializations/java-programming).
You will need a computer with a stable Internet connection, but you will not need an Android phone - we’ll use free software that you can use to emulate a phone on your computer. We'll use Android Studio as IDE; it is compatible with most computer and operating systems. You can find detailed system requirements here: https://developer.android.com/sdk/index.html#Requirements.
*About Project-Centered Courses: Project-centered courses are designed to help you complete a personally meaningful real-world project, with your instructor and a community of learners with similar goals providing guidance and suggestions along the way. By actively applying new concepts as you learn, you’ll master the course content more efficiently; you’ll also get a head start on using the skills you gain to make positive changes in your life and career. When you complete the course, you’ll have a finished project that you’ll be proud to use and share.
Time: 10 hours of study, 10 hours of active project work

講師

Dr. Virginie Galtier

Michel Ianotto

字幕

In this video, I'll show you how to compose an application with multiple activities. We'll be using the example from the Block08 application. In this application the first screen displays a rating bar. You can select your number of stars in it, and then when you click on the button, you're directed to a second activity. And you see here that the information about the number of stars which were selected has been passed to the second activity. And when you click the back button, you don't return to the previous activity but instead to the main screen of the phone. Start a new Android studio project. Call the application Block08. Click Next. Keep the API level 16, click Next. Peek an empty activity, next. And here change the name for the activity that's created: FirstActivity and click finish. First I create the graphical user interface for the first activity. I don't need this TextView here and instead I want a RatingBar. I double click to edit the properties. I want 3 stars in it and I want the rating to be an integer number like one, two, three. So, the stepSize is 1, okay. I also need a button which reads "click here to enter". Now let's define the onClick property for this button to be the 'enter' method. Now, let's go back to FirstActivity.java to define this method. All right, so public void, method name is 'enter' and then it takes a View as a parameter. Before we create a second activity, we'll just display a pop up message which indicates how much stars the user selected. So first I need to retrieve a reference to the rating bar. So let's create our RatingBar object called ratingBar. As usual I use a findViewById method to retrieve the reference for the view that we've defined in the XML file R.id.ratingBar. Okay let's now create an object to hold the rate. The float 'rating'. I can retrieve the current value selected in the rating bar using ratingBar.getRating. And let's create a toast to display this value. Tost.makeText, get the application context, and then the message will be, 'rating' and "stars". And I want to display that for a short amount of time. ... Okay, and let's show this toast. All right, let's try that on the emulator. If I don't select any star and I click on the button I've got the toast: "0 stars". And then if I select, let's say, two of them, and I click on the button, the pop-up reads "2 stars". Okay, but what we want now is not a pop-up message but rather we want a second activity to open and we want to display a message with the number of stars in it in the other activity. Let's go back to the code. First we need to create a second activity. In order to do that, right click on the app here, select New, Activity, Empty Activity. Let's call this activity SecondActivity. And click finish. In the graphical user interface of this second activity, you will just have a large text. "Welcome to second activity!", for now. Okay so now we want to move from the first activity to the second one. So let's go back to FirstActivity.java, and in here, in the enter method, we'll specified, using an object which represents an action to perform, that we want to start the second activity. So first, let's remove the toast message, and create an Intent. So intents are objects that represent operations to be performed. So in our case, this intent represents our will to start the second activity. Let's call it 'goToSecond'. Go, to, second. Okay, I'll create that, it's a new Intent. Okay, and it will be a specific intent: ["explicit intent"] I specify the activity I want to see started. So in order to do that, I use the setClass method. goToSecond.setClass and provide the context, and the class I want to see started is the SecondActivity.class. And when this is done the only thing I have left to do is to use startActivity and provide the intent as a parameter. Okay, let's try that on the emulator. Now when I click the button I move to the second activity. What we need to do now is to pass the rating information from the first activity to the second. So we need to add something here to our intent. I'll use a putExtra method. You see that you have a lot of them, and it's a little like the preferences: you provide a key and a value. The first parameter is a key. And you see that you can provide different kinds of value. And here we want to provide the float 'rating'. putExtra, and then my key will be for instance 'nbStars' and the value is 'rating'. And that's it. Now, in the second activity. First, I need to retrieve this intent in order to be able to get the value from it. So, I'll create an intent, let's call it 'caller'. So that will be the caller, okay. And from that intent I can retrieve the value which was set up with the putExtra using the key, so the key was 'nbStars'. I can create a float, let's call it 'rating' as well. caller.getFloatExtra. The key is 'nbStar'. I don't remember if I put an 's'. Yes. 'nbStars' and we need a default value, let's see... 0. And I want to display this rating in the TextView. So first I need to retrieve the TextView. TextView I use a findViewById method as usual. R.id.textView And now I can change the text to include the rating in it. textView.setText "Welcome to the second activity! Your rating... and I concatenate the value we've retrieved. Okay, let's try that. Let's say I select three stars, I click here to enter. I arrive on the second activity and you can see that the information was correctly passed along. Now, let's move back to the first activity and select maybe only one star. If I click on the button again, you see that the new value was provided. But maybe you want the application to close completely when you hit the back button here and you don't want to go back to this screen anymore. So in order to do that in the first activity after you've started the second one, you just call the finish method. All right, let's run that. I go to the next activity, and when I hit the back button, I go back to the main screen and not the first activity anymore. In this video you've learned how to build an application with multiple activities. First, you need to create a second activity, so you can do that by right clicking on 'app' then select 'New' and 'Activity'. And in the first activity, you need to create an explicit intent to launch the second activity. You do that by creating an Intent object and next you specify the activity to launch using the setClass method. When this is done, you call the startActivity method, providing the intent as a parameter. When the second activity is started, if the user clicks on the back button of the phone, you will return to the first activity. If you don't want that, if rather you prefer to have the first activity suppressed from the activity stack, then after starting the second activity, you call the finish method. You've also learned how to pass information between activities. In the first activity you add the value that you want to pass to the second activity using the putExtra method. The putExtra requires a key which will identify the data that you want to transmit and the value that you want to transmit. The second activity upon starting can retrieve the value that you've transmitted from the first one by first retrieving the reference to the intent, which caused the launch of the second activity, and then on this intent you can call the getFloat or getStringExtra depending on the kind of value that you've passed and then you provide the key and a default value.