Android Programming: The Big Nerd Ranch Guide, 4th Edition
by Bill Phillips, Chris Stewart, Kristin Marsicano, Brian Gardner
For the More Curious: Singletons
The singleton pattern, as used in the CrimeRepository, is very common in Android. 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 will exist as you move between activities and fragments in your application.
Singletons also make a convenient owner of your model objects. Imagine a more complex CriminalIntent application with many activities and fragments modifying crimes. When one controller modifies a crime, how would you make sure that updated crime was sent over to the other controllers?
If the CrimeRepository ...
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