Examples of why encapsulating crimefragments is a good idea?


Hello Everyone,

First of all great book. I have been a big fan of big nerd ranch books and was eagerly awaiting the android book since 2012. Thank you guys for releasing this awesome resource.

I am having a hard time trying to understand why encapsulating crimefragments is a good idea. Dont get me wrong i understand that encapsulation is in general good programming practice but how does it apply to this scenario?

I say this because initially we were calling the following code to get our UUID.

So a call was made to the activity which hosted the fragment to recieve its intent and display the relavent information. However this ofcourse breaks encapsulation.

Then the “fix” was to create a new static method newInstance(UUID crimeId). This method was then called in the CrimeActivity.java class in the onCreate function. The extra was taken out of the intent in the onCreate function and passed into this method to create the fragment.

So my question is how does it help us in our situation? My fragment still expects a crimeId to be able to instantiate, which means any activity that hosts it would have to have that information (intent extra?) to call it.

Sorry if im not getting it. Any help will be greatly appreciated.


You’re right that it doesn’t fix a problem right now. But - well, it’s like brushing your teeth. It doesn’t fix any problems, but good hygiene prevents problems later on.

In this case, you’ll see in the very next chapter that it saves you some work - now that you’ve written CrimeFragment correctly to use arguments, integrating it into ViewPager (and any other context) will be a snap.