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 (Retired) Finishing the User Interface Loading the First Page

jrabello
jrabello
17,917 Points

Creating a Story Instance

Hello,

In this nice video Ben Jakuben creates a new instance of Story in this way:

...
private Story mStory = new Story();
...

My question is:

1 - Is this really considered a good practice? I mean, shouldn't we separate member declarations from instantiations?

2 - I've been learning Java recently, and I'm not really sure where is the moment that this piece of code runs, so, it runs when OnCreate() is called? If so, shouldn't we put it inside OnCreate() ?

3 - I`d like to know if this piece of code runs on the same thread that the StoryActivity ui thread?

1 Answer

Gavin Ralston
Gavin Ralston
28,770 Points

It might be that the Story is instantiated in the class variable declaration because it isn't related to the view, which are all instantiated inside the onCreate() method and related to the layout in some fashion (Views, Buttons, etc).

Then again, I haven't gotten to the Activity Lifecycle stuff, so maybe it'll be explained in further detail there, where you see what specific uses for onCreate, onPause, onClose, etc have, and whether or not it's appropriate to instantiate everything in the onCreate method or not.