Skip to main content

Implement Safe Canceling

Lesson 9 from: Developing Android Apps with Java

Tony Hillerson

Implement Safe Canceling

Lesson 9 from: Developing Android Apps with Java

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

9. Implement Safe Canceling

Next Lesson: Homework and Q & A

Lessons

Class Trailer

Day 1

1

Overview: What is Android?

19:56
2

Eclipse and SDK setup and build a Red Flashlight app

38:05
3

Ad a button to a Green Flashlight

21:06
4

Add the Green Flashlight

18:48

Day 2

5

Week 1 - Layout and Build a Task Manager App

04:10
6

Displaying a list of tasks

40:44

Lesson Info

Implement Safe Canceling

Okay, So now there's one other thing that we want to implement here. And, um that is to make sure that whenever the user has typed in some text ah, if they cancel, we want to make sure that they really wanted to cancel because maybe they accidentally hit the wrong button or something just a little bit of, ah, kind of a safety mechanism. But also, it's a nice, easy way for me to show you how a dialog box works. So what we're gonna do is whenever cancel is is hit, we're gonna check to see if there's any text inside that that ah task and ah, field name. And if there is, then we'll present a dialog box to say, Make sure you sure that you want Teoh uh, get rid of this. You've got someone save changes here. Basically, do you want to go ahead and add the task from here, or do you want to discard it or cancel and try to figure out what you want to do from the same view? The steps we need to do our Ah, we need to add something that's called a text watcher to see if there's any text that has bee...

n input into that, um, text box. And this is There's another simpler way to do that, given that there's only one form field. But I want to show you this way of doing it, Um, because it's it's more. It's more scalable. So you can ADM or form elements to this view and still make things work and also use it as an update screen later. If you wanted to say edit a task and we'll, we'll discuss that in a second and then if any text has been changed in that form field, then we will build on alert dialogue and then present the user with that dialogue and allow them to either add the task that they entered the text for, or discard the changes or or just cancel and dismiss the dialog. Okay, and all this is going to happen on the ad task activity. So let me switch back there, collapse these imports and then, um, start setting up the view a little bit more. So what I want to do in this set of views method, just like I added some click listeners in the ad task activity set of views that we're looking at. I want to set up what's called a text changed listener. So this is another type of listener that's listening for some user input. Um, pasting in some code for it right now. Then we could talk about it. Eso a text change listener can be added to the component type of an edit text. Um, and what you do is add what's called a text watcher. Import that text watcher. Okay. Looks like there. I wrote this code during 1. and now 1.6 added another piece today to the to the interface. So text watchers interface that defines a couple different methods. What's weighed? I guess that's not true. It didn't. Ah, didn't add one. I don't know why Eclipse thought that I had in implementing this method, but I sure did start there. Just nothing happened inside of there. Okay, so, again, the text watchers, an interface that defines a couple methods, um, that will be called during certain during different points of the user entering some text or the text changing inside of ah, unedited, edible text component. Um, and those hooks are onto exchanged before to exchange and after text change. So you get to hook into whenever a user is typing into an edit text field before they before the text changes are allowed. So you could do you use this to do something like disallowing certain characters? Um, after text changed, you could hook in here and do some changes to the text as a whole may be doing. Maybe you would call a spell checker hook or something like that. Teoh, if you wanted to. Your own spell checking, but on text changed. What we're gonna do, which is when the text is actually changed, is we're just gonna set up. This change is pending flag, which we want to be a field on add task activity. Someone hover over here and click this and it will create that code appear at the top. Basically, this ad task activity is now gonna track whenever there are changes to any of the form elements on the screen. And the reason I say this is a little more scalable is I could also just check to see whenever the user wanted to leave this screen. If there was any text inside the add text ah or the task Name a text field. But what if I wanted to repurpose the screen as a as a way to edit on existing task? Then I would already have test text in their off, the off the bad. So in this case, I want to track if there are any changes pending instead and that's a little bit more flexible way to do it. So basically, this text changed listener to recap is gonna watch for any changes to the text inside that edit text and then say whether they're changes and we'll be able to reference this variable to our benefit here inside the cancel method. So we're not just gonna finish right off the bat once the user presses the cancel button? Um, we're going to check first to see if there any changes pending. So if change is pending, do a little bit of thought before we just let them go ahead and finish. And I put this inside one else. Okay, so what we want to do if there are some changes pending is present the user with the dialog box that we looked at and our slide cops here, so I need to have a title of unsafe changes and the description or the text needs to be. Would you like to add this task? And then I want there to be three buttons on the dialogue and this is sort of Ah, this is a good practice to follow whenever you create dialogue buttons, um, Apple gets this right, and it's I think it's in their hat there. They're human interface guidelines. Um, a good practice to follow is just to expect that the user is not gonna is always gonna be either either. Not gonna want to Teoh read your message in your dialog box right away, Um, or be confused by reading it. If they look at the buttons and they see some sort of generic like OK or cancel, they're gonna be confused by the whole process. So it's better to to have buttons that are labeled with what you are allowing the user to do. So we're not going to say OK or cancel or something generic like that. We're going to say what the buttons actually to do. So each one of these buttons need to be labeled with the action that the user's gonna take if they press the button, and those two actions are add task or discard, and then cancel will just be a simple dismiss the dialog, um, Button. So I'm gonna paste in some code here to save some typing again and then describe what I'm doing. So is anybody familiar out there and the audience with what's called the builder pattern? You see this a lot in Apache code. This is just sort of a kind of raise your hand type of situation. We don't have any hands up from the audience. Nobody is commenting and goto webinar Okay, so I'll describe this a little bit. The builder pattern just a nisi way to build an object with instead of constructing the object and then saying a bunch of properties. It's sort of like a kind of a simpler than a factory method type of a thing. But I'm creating this is New Class or an instance of this alert dialogue builder, Um, and then that takes a context, which is just this in this case. But then noticed, I'm going on to call a bunch of methods on the object that's returned to their so this kind of a nice pattern to set up um, a, uh a process where you can construct an object and set a bunch of attributes about the object during this This ah, the process where we're calling new methods on the builder. And then at the end of that whole process, you call create and the actual object is created. So the sun save changes. Dialogue is going to be a type. Um, I'm gonna create a field out of it. Um, but it's probably gonna get the type wrong. It's going to say object, but I want it to be an alert dialogue. And so, Tony theologian dialogue is just structured so that it uses the builder pattern in order to to create all of the pieces of it. Yeah, well, it just helps me build alert dog logs so I could create. I think I can just create an alert dialogue on my own by saying new a dirt alert dialogue, you know, using its constructor. But then I'd have to set up some things myself and go into this builder, sort of prompt you here. The different things that you can do on it. It's just sort of a different way of setting up an object with some defaults before it's actually created. So I'm creating a new builder. But once I press create, it's gonna actually return the type of alert dialogue just bears a little bit of explanation there. So, um, a couple things that I want to do to this builder are our tea set up with the title is gonna be what the message going to be. And then there is this sort of interesting, um, process here that I set up the click listeners of the different buttons and also tell the builder how many buttons I want. I want to have a positive button, neutral button and a negative button. And those air those kind of defer or delegate to the sdk the positioning of where, um, as a standard, the user is always going to see the positive selection and the negative selection. So it was like a okay, cancel situation. You would say I want a positive button and a negative button, but you wouldn't say you want a neutral button. And then the positioning, like left or right, is is standardised by the sdk, so you don't have to worry about Welsh. I put the the okay. Button on the left are shape Adobe's OK, button on the right. And where were the neutral button? Go? Um, that's off all sanitized by using this builder pattern as well. And we want our positive button to be yes, go ahead and add the task that I had typed in and what the neutral button to be, Um ah, in the middle. Well, it's it's gonna be in the middle by the sdk, but what we want to do is just finished the activity at that at that at the instance, the user clicks on that button and then the cancel button. We want that to just cancel out the dialogue, which is accomplished by calling unsafe changes. Dialogue, not cancel. That just gets rid of the dialogue. But that's pretty much working the way we needed to. But we need to look at the it looks like eclipses complaining about this one again. We get rid of this, see if that makes it happy. Okay, so this negative button is all set up. That's the cancel button switch back here to the slides. That's the cancel button over there on the right. That one is doing what we wanted to do. Um, but you can see that there's a couple of resource is that we need to create here, and then I'll go through and talk through what else is going on? Um, so setting the title is gonna take a resource, and we want that, um, to be set up inside the strings file. So, in unsafe changes title I'm gonna copy that key and then open resource strings and duplicate this line on save Save changes. Title were to say, just like the cops say on save changes on that I need another one for, uh, unsafe changes message, which is, as the cops say, Would you like to add this task? Change the strength? And then there was also a discard, so I will add one for that. Now, those researchers sources air there than this. There's no longer any problems here, and we'll set up. So the set positive button add task will call add task, just as if the user to click the add task button and the neutral button instead of calling cancel even though it's gonna be labeled, Uh, well, it's gonna do the same thing as with what the cancel button used to do. It's just gonna call finish because we don't want to get into an influence loop here. So that's gonna be the finish path. And then the neutral button will once again just dismiss the dialog. So it's go give that a try running this from the big play button switch or the emulator. It should be loading up here in a second. Now, if I come here no notice again. Once since we restarted the application that the memory was gone, everything was gone out of memory. So any task that I entered before no longer there when I type good milk accidentally hit Cancel instead. We didn't know where that up yet. So what we need to do is ah, you know, we did wire that up. So why is that not working? Let's do a little bit of checking here. So I want to make sure that this on text change, um is actually getting called. So I'm gonna put a break point in here, and we're on the application in debug mode. So now, once I covered add task and I hit get milk, It should open up in our d bugger. And yes, that's true. Doing open this perspective now. Yes. So now I am in the debugging the Java debugging perspective. Well, once eclipse wakes up, I will be at least okay, And I'm inside of the click listener, and I should be setting changes pending to true. So once I step over, this changes pending. If I hover over this, um, well, it's pointing to the right place, but I can go up inside of ah ah, actually, can't get a reference to that to the ah, the activity. Oh, at tax activity, I might be able to get up into that. So this zero are dollar signs, ears and add tacked add task activity. And there's my variable changes pending is true. Um, and then when cancel is clicked, If change is pending, then I should show this dialogue. Ah, one. That's the problem with cutting and pasting or from not knowing what your code supposed to do. There's one step that I missed. So just let me act. Um, maximize this here. Um, just going through and creating the dialogue wasn't enough to actually show it. So the problem was that I needed to call One more thing and that is UN changes, unsafe changes, dialogue, not show. So now everything should be working on. We get this same if I I saved the activity and I get this error that says I can't swap the code and running on the VM. Um, so if you're ever worked with the Java and another situation, like maybe on the server, you can actually save changes inside of the clips and have those changes automatically uploaded to the running virtual machine and swapped out. But you can't do that inside the android virtual machine. So I had to say, Disconnect from the d bugger. And now if I run this again, we should actually get our dialogue showing up once the application starts over. That was all plants that you could see. The d bugger in action again. Right? Okay, so now when I say get milk, I accidentally it cancel instead of add task, I'm asked what I like to save this task. If I say cancel the dialogue goes way, cancel again and I say, Discard, nothing is added. But if I say instead, add task, then it says, just as if I had the ad task button. And now we have a little bit of a guard against accidental user input. So are there any questions? I'm sure someone want to see the code. Here, let me switch back. Two clips show the cancel method here. Any questions about what we did so far there? Yes. We have a question from the chat room. Somebody was wondering about the change spending, uh, flag And where that is set. Originally in the coat, they're getting a little bit lost in like, Ah, well, that's just a job. A thing. I I define it here online. 19 of ad task activity. Um, whenever you define a boolean, it's automatically initialized to false. That's a job thing. So I didn't initialize it anywhere else. I have another question about what's the difference between using the cancel button and going using the back button on the device. So, um, we didn't hook this in because, um, I'm gonna be dealing with, um, or specific, like, moving around type stuff. Well, I guess I could talk through it cause it's fairly simple. The difference between hitting the cancel button and it hitting finish and you hitting the back button is pretty much for all intents and purposes, the same thing. It's just that the in our interface that we set up here me switch back here. So if I say if I sake, um, hit the back button here, it's not gonna go through that whole process where he had to cancel on the edit text change listener. Oh, you don't check to see if it's blank so that allow your currently it'll allow an empty task to be displayed. Ah, yeah. Good point. Yeah, just an oversight. So basically, Ah, we could do something like if changes pending and ah, I have to get this whole ugly, ugly string together. Or is that task here? Task name, um equals empty string? Not okay. So if change spending and and it's not blank, then go ahead with this. Otherwise allow it. So, yeah, that was just an oversight. Good catch. Why don't you finish up then and then we'll come back Teoh some of the other remaining questions so that we can finish up on time. Okay, so, basically, he's just finishing up. Um, these are the files that we added this time we had ah well during this this second process because we didn't look at the ones we added. Since then, we have the new activity for at task and then we have the super class task manager Activity and new layout add tasking FedEx, Mel.

Ratings and Reviews

a Creativelive Student
 

I thought everything on this site was free and now they want 99 dollars for a course

a Creativelive Student
 

very good

Student Work

RELATED ARTICLES

RELATED ARTICLES