Android Programming: The Big Nerd Ranch Guide, 5th Edition
by Bryan Sills, Brian Gardner, Kristin Marsicano, Chris Stewart
For the More Curious: Singletons
The singleton pattern, as used in the CrimeRepository, is very common in Android. But singletons get a bad rap because they can be misused in a way that makes an app hard to maintain.
Singletons are often used in Android because they outlive a single fragment or activity. A singleton will still exist across rotation and as you move between activities and fragments in your application.
Singletons also make a convenient owner for your model objects. Imagine a more complex CriminalIntent application with many activities and fragments modifying crimes. When one component modifies a crime, how would you make sure that updated crime was sent over to the other components?
If the CrimeRepository is the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access