Please see this thread of Android API FAQ, here you will surely find the most appropriate solution for your problem (whether you need a short-living or a long-life data).
Singletons are used for short life-cycle objects.
Edit: Please keep in mind, that for extensive amount of data it is always a better idea to use databases / structurated local files (though the latter are resource-consuming to read/write). And if you are working with very complex object, choosing SharedPreferences
might cause some headache too.
A sample of storing / loading a list from SharedPreferences
:
// the name with which to store the shared preference
public static final String PERSON_LIST = "my.personlist";
// a sample data structure that will be stored
public static final class Person
{
private String name;
private int age;
public String toString()
{
return name + "|" + age;
}
// splits the passed String parameter, and retrieves the members
public static Person loadPersonFromString(final String personString)
{
final Person p = new Person();
final String[] data = personString.split("|");
p.name = data[0];
p.age = Integer.parseInt(data[1]);
return p;
}
}
/**
* Saves the current list of Persons in SharedPreferences
* @param persons: the list to save
*/
public void savePersonList(final ArrayList<Person> persons)
{
final SharedPreferences prefs = PreferenceManager.
getDefaultSharedPreferences(getApplicationContext());
final Editor editor = prefs.edit();
final StringBuilder builder = new StringBuilder();
for (final Person person : persons)
{
if (builder.length() > 0)
builder.append("-Person-");
builder.append(person.toString());
}
editor.putString(PERSON_LIST, builder.toString());
editor.commit();
}
/**
* Loads the list of Persons from SharedPreferences
* @return the loaded list
*/
public ArrayList<Person> loadPersonList()
{
final SharedPreferences prefs = PreferenceManager.
getDefaultSharedPreferences(getApplicationContext());
final ArrayList<Person> persons = new ArrayList<Menu.Person>();
final String[] array = prefs.getString(PERSON_LIST, "").split("-Person-");
for (final String personString : array)
{
if (personString.length() > 0)
persons.add(Person.loadPersonFromString(personString));
}
return persons;
}