Finishing an Activity2:40 with Ben Jakuben
First, let's take a look at how to properly finish an Activity to return to the original Activity that started it. Then we can talk about how to handle a user's name when a story is restarted.
All right, so how are we going to get back to the start of our app? 0:00 Let's add a new OnClickListener for this play_again_button. 0:03 And then we can explore our options. 0:06 So choice2Button.setOnClickListener(new OnClickListener), and there we go. 0:09 Now, to get back to MainActivity, 0:16 we could use a brand new intent, just like we've seen before. 0:18 Except this time, it would be going the other way. 0:21 We would be starting the MainActivity from our StoryActivity, 0:23 and it would technically work, but it would actually be adding a new activity on 0:26 something behind the scenes called our navigation stack, or back stack. 0:30 More on that in just a moment. 0:34 We actually all ready have a relationship with MainActivity here. 0:35 We started StoryActivity from MainActivity. 0:38 So, that means we can go back to it with a very simple activity method. 0:40 We can call finish here, which will finish the current activity and get rid of it, 0:43 taking us back to the existing MainActivity, 0:47 which might even still be in memory from before. 0:50 Okay, so we come in here, add a new name, start the adventure and 0:55 navigate all the way to the end of the story. 1:00 And if we hit Play Again, it goes back to beginning. 1:03 Cool, all right. 1:06 So, this works well but I'd like to improve it slightly and 1:06 make our app navigation a little more robust. 1:09 When a user taps on Play Again, do we want to restart the story with the same name or 1:12 do we wanna go all the way to beginning and have the user enter a new name? 1:16 We could do both. 1:20 Let's change this to have the play again button restart the story with 1:21 the same name at page zero. 1:24 And then in the next video, we can talk briefly about Android app navigation and 1:26 how to get all way back to the MainActivity in a different way. 1:30 So, to restart the story, all we need to do is load page zero again. 1:33 We can delete this finish line, or comment out if you wanna save it for reference. 1:37 And then type loadPage(0). 1:41 We could test this, but we're going to have a problem. 1:44 Can you guess what it is? 1:47 All right, let's take a look. 1:48 All right, so we'll do the story, go through to the end, 1:52 click on Play Again, and look at that, we have a missing button. 1:56 It's still invisible. 2:00 Okay, this is an easy fix. 2:02 We can just make sure our buttons are visible each time we load them. 2:04 So, here in our fancy new loadButtons method, let's add a line at the top. 2:07 Choice1Button.setVisibility, and 2:12 we'll just set it to View.VISIBLE each time through. 2:16 And we can do the same thing for choice2, choice2Button.setVisibility, View.VISIBLE. 2:20 Let's just make sure this is working. 2:28 Okay, start the adventure. 2:32 Go to the end, Play Again. 2:34 And now we have two buttons working, very cool. 2:36
You need to sign up for Treehouse in order to download course files.Sign up