Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App The Rest of the Story Ending the Story

I don't understand the Listeners added to Buttons

We added Listeners to Buttons. When we hit them it takes us to relevant pages (nextPage). But I don't understand how these Buttons know the path which we must go? We have no any methods that update the nextPage variable. Can you please widely explain the process behind the " loadPage(page.getChoice1().getNextPage());" method?

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

Check the Story constructor, it's where we are creating all the Page objects. And each Page object has two Choice objects, and each Choice object has a textId and nextPage. The nextPage actually contains the number of the page we should go to for the respective choice. And all that info is filled in the constructor, we copy pasted it from teacher's notes (most of it), which might lead to your confusion that we didn't add this information and getting that "how does the app now where to go?" feeling :).

For example, this line in the Story constructor

pages[0] = new Page(R.drawable.page0,
                R.string.page0,
                new Choice(R.string.page0_choice1, 1),
                new Choice(R.string.page0_choice2, 2));

is creating a new Page object, adding it at index 0 of the pages array, with the 3rd and 4th parameters being Choice objects which we are creating right then and there, and the second parameter for each Choice object is a number which indicates to which page we should go if that choice is selected. So all the information about how to navigate through the story is already entered in the Story constructor.