From the course: Android Development Tips

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Manage global data with singletons

Manage global data with singletons - Android Tutorial

From the course: Android Development Tips

Manage global data with singletons

- [Instructor] Mobile application developers in general and Android developers in particular, have to pay a lot of attention to state management. Holding on to data when the device orientation changes or other things change that can make you lose references to objects. I'm going to show you a very simple technique that makes data persistence a lot easier. It let's you persist data not just through configuration changes, but also sharing that data among different activities and services and other components of your application. I'm going to use a simple Java singleton class. A class for which there's only one possible instance. Now, you'll see similar recommendations using something called the application context. In Android the application context is a singleton, but for data persistence it's no better or worse than just a simple singleton you can create yourself. Right now in this application, I'm collecting a bit of information from the user with a dialogue. And then, I'm saving it…

Contents